Guava的只读、函数式编程、集合——高淇JAVA300讲笔记之Guava
Guava简介
Guava是谷歌工程师开发的集合库,它是对jdk提供的扩展,提供了很多实用的类来简化代码。
一下例子使用的是guava-20.0版本。
案例一:只读设置
1 package com.bjsxt.others.guava; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.List; 6 7 import com.google.common.collect.ImmutableList; 8 9 /** 10 * 只读设置 11 * 12 */ 13 public class Demo01 { 14 public static void main(String[] args) { 15 List<String> list = new ArrayList<String>(); 16 list.add("a"); 17 list.add("b"); 18 list.add("c"); 19 20 //对原有的list进行包装,相当于原有List的一个视图,快照,不够安全 21 List<String> readList = Collections.unmodifiableList(list); 22 //java.lang.UnsupportedOperationException 23 // readList.add("d"); //会报错 24 list.add("d"); //改变原有List 视图也一起改变 25 26 //对比查看 初始化List guava对只读设置 安全可靠,并且相对简单 27 List<String> immutableList = ImmutableList.of("a","b","c"); 28 immutableList.add("d"); //java.lang.UnsupportedOperationException 29 } 30 }
案例二:函数式编程:解耦
1 package com.bjsxt.others.guava; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Collection; 5 import java.util.List; 6 import java.util.Set; 7 8 import com.google.common.base.Function; 9 import com.google.common.base.Functions; 10 import com.google.common.base.Predicate; 11 import com.google.common.collect.Collections2; 12 import com.google.common.collect.Lists; 13 import com.google.common.collect.Sets; 14 15 /** 16 * 函数式编程:解耦 17 * 1、Predicate 18 * 2、Function 19 * 20 * 工具:Collections2.filter() 过滤器 21 * Collections2.transfer() 转换 22 * Functions.compose() 组合式函数编程 23 */ 24 public class Demo02 { 25 public static void main(String[] args) { 26 //组合式函数编程 27 //确保容器中的字符串长度不超过5,超过进行截取,后全部大写 28 List<String> list = Lists.newArrayList("bjsxt","goods","happiness"); 29 //确保容器中的字符串长度不超过5,超过进行截取 30 Function<String,String> f1 = new Function<String,String>(){ 31 32 @Override 33 public String apply(String input) { 34 return input.length()>5?input.substring(0, 5):input; 35 } 36 37 }; 38 //转成大写 39 Function<String,String> f2 = new Function<String,String>(){ 40 41 @Override 42 public String apply(String input) { 43 return input.toUpperCase(); 44 } 45 46 }; 47 48 //String = f2(f1(String)) 49 Function<String,String> f = Functions.compose(f1, f2); 50 Collection<String> resultCol = Collections2.transform(list, f); 51 52 for(String temp:resultCol) { 53 System.out.println(temp); 54 } 55 56 } 57 /** 58 * 过滤器 59 */ 60 public static void test1() { 61 //创建List 静态初始化 62 List<String> list = Lists.newArrayList("moom","son","dad","bjsxt","refer"); 63 //找出回文 palindrome backwoeds mirror words(指单词正着写和倒着写是一样的) 64 //匿名内部类对象:匿名内部类,同时创建类对象 65 Collection<String> palindromeList = Collections2.filter(list, new Predicate<String>() { 66 67 @Override 68 public boolean apply(String input) { 69 //业务逻辑 70 return new StringBuilder(input).reverse().toString().equals(input); 71 } 72 73 }); 74 75 for(String temp:palindromeList) { 76 System.out.println(temp); 77 } 78 } 79 80 /** 81 * 转换 82 */ 83 public static void test2() { 84 //类型转换 85 Set<Long> timeSet = Sets.newHashSet(); 86 timeSet.add(10000000L); 87 timeSet.add(9999999999990099L); 88 timeSet.add(200000000000L); 89 90 Collection<String> timeStrCol = Collections2.transform(timeSet, new Function<Long,String>() { 91 92 @Override 93 public String apply(Long input) { 94 95 return new SimpleDateFormat("yyyy-MM-dd").format(input); 96 } 97 98 }); 99 100 for(String temp:timeStrCol) { 101 System.out.println(temp); 102 } 103 } 104 }
test1的运行结果:
moom
dad
refer
test2的运行结果:
318857-05-21 1976-05-04 1970-01-01
主函数的运行结果:
BJSXT
GOODS
HAPPI
案例三:约束,Constraint,这个类在新版本里已经删除了。
案例四:集合的操作:交集、差集、并集
1 package com.bjsxt.others.guava; 2 3 import java.util.Set; 4 5 import com.google.common.collect.Sets; 6 import com.google.common.collect.Sets.SetView; 7 8 /** 9 * 集合的操作:交集、差集、并集 10 * Sets.intersection() 11 * Sets.difference() 12 * Sets.union() 13 * 14 * 15 * 16 */ 17 public class Demo04 { 18 public static void main(String[] args) { 19 Set<Integer> sets = Sets.newHashSet(1,2,3,4,5,6); 20 Set<Integer> sets2 = Sets.newHashSet(3,4,5,6,7,8,9); 21 22 //交集 23 System.out.println("交集为:"); 24 SetView<Integer> intersection = Sets.intersection(sets, sets2); 25 for(Integer temp:intersection) { 26 System.out.println(temp); 27 } 28 //差集 29 System.out.println("差集为:"); 30 SetView<Integer> diff = Sets.difference(sets, sets2); 31 for(Integer temp:diff) { 32 System.out.println(temp); 33 } 34 //并集 35 System.out.println("并集为:"); 36 SetView<Integer> union = Sets.union(sets, sets2); 37 for(Integer temp:union) { 38 System.out.println(temp); 39 } 40 } 41 }
运行结果:
交集为: 3 4 5 6 差集为: 1 2 并集为: 1 2 3 4 5 6 7 8 9