Guava中的集合是非常的强大,使用起来确实很方便,性能也有提高,日常关于集合的相关操作,推荐使用guava,所有想要的功能都能实现,以下是常见的集合操作,如果有需要高级用法的需求,参照:http://ifeve.com/google-guava/
//构建不可变集合
final ImmutableSet<String> COLOR_NAMES = ImmutableSet.of(
"red", "orange", "green", "yellow");
System.out.println(COLOR_NAMES);
final ImmutableSet<String> COLOR_NAMES_NEW = ImmutableSet.copyOf(COLOR_NAMES);
System.out.println(COLOR_NAMES_NEW);
//有序不可变集合
ImmutableSortedSet<String> sort_set_1 = ImmutableSortedSet.of("a","d","b","a","e");//输出:[a, b, d, e]
ImmutableSortedSet<String> sort_set_2 = ImmutableSortedSet.of("2","1","4","3","2");//输出:[1, 2, 3, 4]
System.out.println(sort_set_1);
System.out.println(sort_set_2);
//统计单词在文档中出现次数
HashMultiset<String> hm = HashMultiset.create();
hm.add("a");
hm.add("b");
hm.add("a");
hm.add("a");
hm.add("e");
System.out.println(hm.count("a") + "---" + hm.count("b"));//输出3---1
//新的静态工厂方法,可以避免重复的生命泛型等等
List<String> list = Lists.newArrayList();
List<String> list_new = Lists.newArrayList("a","b");//指定起始元素
Map<Integer,String> map = Maps.newConcurrentMap();
//Lists工具方法
List<Integer> countUp = Ints.asList(1,2,4,5,6);
List<Integer> countDown = Lists.reverse(countUp);//输出:6, 5, 4, 2, 1
System.out.println(countDown);
List<List<Integer>> parts = Lists.partition(countUp, 2);//输出:[[1, 2], [4, 5], [6]]
System.out.println(parts);
//Sets工具方法
Set<String> firstSet = ImmutableSet.of("a","b","e","f","g");
Set<String> twoSet = ImmutableSet.of("a","c","e","h");
SetView<String> union = Sets.union(firstSet, twoSet);//输出:[a, b, e, f, g, c, h]
System.out.println(union.immutableCopy());
SetView<String> intersection = Sets.intersection(firstSet, twoSet);//输出:[a, e]
System.out.println(intersection.immutableCopy());
SetView<String> diff = Sets.difference(firstSet, twoSet);//输出:[b, f, g]
System.out.println(diff.immutableCopy());
//Maps工具方法
Map<String,Integer> left = ImmutableMap.of("a",1,"b",2,"c",3);
Map<String,Integer> right = ImmutableMap.of("e",1,"b",2,"c",6);
MapDifference<String, Integer> diffs = Maps.difference(left, right);
System.out.println(diffs.entriesInCommon());//输出:{b=2}
System.out.println(diffs.entriesDiffering());//输出:{c=(3, 6)}
System.out.println(diffs.entriesOnlyOnLeft());//输出:{a=1}
System.out.println(diffs.entriesOnlyOnRight());//输出:{e=1}