1.Lists
1 //Lists
2 System.out.println("### Lists ###");
3 ArrayList<String> arrayList = Lists.newArrayList();
4 arrayList.add("A");
5 arrayList.add("B");
6 arrayList.add("C");
7 System.out.println(arrayList);
8
9 LinkedList<String> linkedList = Lists.newLinkedList();
10 linkedList.add("A");
11 linkedList.add("B");
12 linkedList.add("C");
13 System.out.println(linkedList);
14
15 System.out.println(Lists.reverse(arrayList)); //翻转集合元素的顺序
16 System.out.println(Lists.reverse(linkedList));
17
18 System.out.println(Lists.partition(arrayList, 2)); //一个集合内再分成n个子集合
2.Sets
//Sets
System.out.println("### Sets ###");
HashSet<String> set1 = Sets.newHashSet();
set1.add("A");
set1.add("B");
set1.add("C");
LinkedHashSet<String> set2 = Sets.newLinkedHashSet();
set2.add("B");
set2.add("C");
set2.add("D");
set2.add("E");
//Sets.newTreeSet();
//Sets.newConcurrentHashSet();
System.out.println(Sets.union(set1, set2)); //返回两个集合的并集 >> [A, B, C, D, E]
System.out.println(Sets.difference(set1, set2)); //返回set1中不存在于set2的元素集合 >> [A]
System.out.println(Sets.intersection(set1, set2)); //返回两个集合的交集 >> [B, C]
Set<String> resultSet = Sets.filter(set2, new Predicate<String>() {
@Override
public boolean apply(String value) {
return !"D".equals(value); //过滤掉值为D的元素 >> [B, C, E]
}
});
System.out.println(resultSet);
3.Maps
//Maps
System.out.println("### Maps ###");
Maps.newHashMap();
Maps.newLinkedHashMap();
Maps.newTreeMap();
Maps.newConcurrentMap();
4.Collections
//Collections2
System.out.println("### Collections2 ###");
Collection<String> linkedList2 = Collections2.filter(linkedList, new Predicate<String>() {
@Override
public boolean apply(String value) {
return !"B".equals(value); //过滤器:过滤掉值为B的元素 >> [A, C]
}
});
System.out.println(linkedList2);
Collection<String> linkedList3 = Collections2.transform(linkedList, new Function<String, String>() {
@Override
public String apply(String value) {
return value + "_"; //转换器:元素值做特殊处理后返回 >> [A_, B_, C_]
}
});
System.out.println(linkedList3);