guava
package com.bjsxt.others.guava; import java.util.ArrayList; import java.util.Collections; import java.util.List; import com.google.common.collect.ImmutableList; /** * 只读设置 * @author Administrator * */ public class Demo01 { /** * @param args */ public static void main(String[] args) { List<String> list =new ArrayList<String>(); list.add("a"); list.add("b"); list.add("c"); //对原有的list进行包装,相等于原有List的一个视图,快照,不够安全 List<String> readList =Collections.unmodifiableList(list); //java.lang.UnsupportedOperationException //readList.add("d"); list.add("d"); //改变原有List 视图也一起改变 //对比 查看 初始化List guava对只读设置 安全可靠,并且相对简单 List<String> immutableList =ImmutableList.of("a", "b", "c"); immutableList.add("d");//java.lang.UnsupportedOperationException } }
package com.bjsxt.others.guava; import java.text.SimpleDateFormat; import java.util.Collection; import java.util.List; import java.util.Set; import com.google.common.base.Function; import com.google.common.base.Functions; import com.google.common.base.Predicate; import com.google.common.collect.Collections2; import com.google.common.collect.Lists; import com.google.common.collect.Sets; /** * 函数式编程 :解耦 * 1、Predicate * 2、Function * * 工具:Collections2.filter() 过滤器 * Collections2.transfer() 转换 * Functions.compose()组合式函数编程 * @author Administrator * */ public class Demo02 { /** * @param args */ public static void main(String[] args) { //组合式函数编程 //确保容器中的字符串长度不超过5,超过进行截取,后全部大写 List<String> list =Lists.newArrayList("bjsxt","good","happiness"); //确保容器中的字符串长度不超过5,超过进行截取 Function<String,String> f1 =new Function<String,String>(){ @Override public String apply(String input) { return input.length()>5?input.substring(0,5):input; } }; //转成大写 Function<String,String> f2 =new Function<String,String>(){ @Override public String apply(String input) { return input.toUpperCase(); } }; //String =f2(f1(String)) Function<String,String> f =Functions.compose(f1, f2); Collection<String> resultCol =Collections2.transform(list, f); for(String temp:resultCol){ System.out.println(temp); } } /** * 转换 */ public static void test2(){ //类型转换 Set<Long> timeSet =Sets.newHashSet(); timeSet.add(10000000L); timeSet.add(99999999999999999L); timeSet.add(2000000000L); Collection<String> timeStrCol =Collections2.transform(timeSet, new Function<Long,String>(){ @Override public String apply(Long input) { return new SimpleDateFormat("yyyy-MM-dd").format(input); }}); for(String temp:timeStrCol){ System.out.println(temp); } } /** * 过滤器 */ public static void test1(){ //创建List 静态初始化 List<String> list =Lists.newArrayList("moom","son","dad","bjsxt","refer"); //找出回文 palindrome backwords mirror words //匿名内部类对象: 匿名内部类,同时创建类对象 Collection<String> palindromeList =Collections2.filter(list, new Predicate<String>(){ @Override public boolean apply(String input) { //业务逻辑 return new StringBuilder(input).reverse().toString().equals(input); } }); for(String temp:palindromeList){ System.out.println(temp); } } }
package com.bjsxt.others.guava; import java.util.Set; import com.google.common.base.Preconditions; import com.google.common.collect.Constraint; import com.google.common.collect.Constraints; import com.google.common.collect.Sets; /** * 加入约束条件:非空、长度验证 * Constraint * Preconditions * Constraints * @author Administrator * */ public class Demo03 { /** * @param args */ public static void main(String[] args) { Set<String> sets =Sets.newHashSet(); //创建约束 Constraint<String> constraint =new Constraint<String>(){ @Override public String checkElement(String element) { //非空验证 Preconditions.checkNotNull(element); //长度验证 5-20为字符串 Preconditions.checkArgument(element.length()>=5 && element.length()<=20); return element; } }; Set<String> cs =Constraints.constrainedSet(sets, constraint); //cs.add(null); //java.lang.NullPointerException // cs.add("good"); //java.lang.IllegalArgumentException cs.add("bjsxt"); for(String str:cs){ System.out.println(str); } } }
package com.bjsxt.others.guava; import java.util.Set; import com.google.common.collect.Sets; import com.google.common.collect.Sets.SetView; /** * 集合的操作:交集、差集、并集 * Sets.intersection() * Sets.difference() * Sets.union(); * @author Administrator * */ public class Demo04 { /** * @param args */ public static void main(String[] args) { Set<Integer> sets =Sets.newHashSet(1,2,3,4,5,6); Set<Integer> sets2 =Sets.newHashSet(3,4,5,6,7,8,9); //交集 System.out.println("交集为:"); SetView<Integer> intersection =Sets.intersection(sets, sets2); for(Integer temp:intersection){ System.out.println(temp); } //差集 System.out.println("差集为:"); SetView<Integer> diff =Sets.difference(sets, sets2); for(Integer temp:diff){ System.out.println(temp); } //并集 System.out.println("并集为:"); SetView<Integer> union =Sets.union(sets, sets2); for(Integer temp:union){ System.out.println(temp); } } }
package com.bjsxt.others.guava; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; /** * HashMap 键唯一,值可以重复 * BiMap:双向Map(Bidirectional Map ) 键与值都不能重复(unique -valued map) * @author Administrator * */ public class Demo07 { /** * @param args */ public static void main(String[] args) { BiMap<String,String> bimap=HashBiMap.create(); bimap.put("bjsxt", "bjsxt@sina.com"); bimap.put("good","good@qq.com"); //通过邮箱找用户 String user =bimap.inverse().get("good@qq.com"); System.out.println(user); System.out.println(bimap.inverse().inverse()==bimap); } }
package com.bjsxt.others.guava; import java.util.Map; import java.util.Set; import com.google.common.collect.HashBasedTable; import com.google.common.collect.Table; import com.google.common.collect.Table.Cell; import com.google.common.collect.Tables; /** * 双键的Map -->Table -->rowKey+columnKey+value * 1、方法 * 所有的行数据:cellSet() * 所有的学生: rowKeySet() * 所有的课程:columnKeySet() * 所有的成绩: values() * 学生对应的课程: rowMap() +get(学生) * row(学生) * 课程对应的学生: columnMap +get(课程) * column(课程) * @author Administrator * */ public class Demo08 { /** * @param args */ public static void main(String[] args) { Table<String,String,Integer> tables=HashBasedTable.create(); //测试数据 tables.put("a", "javase", 80); tables.put("b", "javase", 90); tables.put("a", "oracle", 100); tables.put("c", "oracle", 95); //所有的行数据 Set<Cell<String,String,Integer>> cells =tables.cellSet(); for(Cell<String,String,Integer> temp:cells){ System.out.println(temp.getRowKey()+"-->"+temp.getColumnKey()+"-->"+temp.getValue()); } System.out.println("==========学生查看成绩=============="); System.out.print("学生\t"); //所有的课程 Set<String> cours =tables.columnKeySet(); for(String t:cours){ System.out.print(t+"\t"); } System.out.println(); //所有的学生 Set<String> stus =tables.rowKeySet(); for(String stu:stus){ System.out.print(stu+"\t"); Map<String,Integer> scores =tables.row(stu); for(String c:cours){ System.out.print(scores.get(c)+"\t"); } System.out.println(); } System.out.println("==========课程查看成绩=============="); System.out.print("课程\t"); //所有的学生 Set<String> stuSet =tables.rowKeySet(); for(String t:stuSet){ System.out.print(t+"\t"); } System.out.println(); //所有的课程 Set<String> courSet =tables.columnKeySet(); for(String c:courSet){ System.out.print(c+"\t"); Map<String,Integer> scores =tables.column(c); for(String s:stuSet){ System.out.print(scores.get(s)+"\t"); } System.out.println(); } System.out.println("===========转换==========="); Table<String,String,Integer> tables2 =Tables.transpose(tables); //所有的行数据 Set<Cell<String,String,Integer>> cells2 =tables2.cellSet(); for(Cell<String,String,Integer> temp:cells2){ System.out.println(temp.getRowKey()+"-->"+temp.getColumnKey()+"-->"+temp.getValue()); } } }