在上一节Stream中提到了Collect方法其实已经说了很多关于Collector的事情,我们这里总结下Collector的一些用法。
Collector一般用于Stream工作流的最后一步的收集。
一些常用的方法
1. Collectors.toList()用ArrayList收集
List<String> givenList = Arrays.asList("a", "bb", "ccc", "dd");
List<String> result = givenList.stream().collect(toList());
2. Collectors.toSet()用HashSet收集
List<String> givenList = Arrays.asList("a", "bb", "ccc", "dd");
List<String> result = givenList.stream().collect(toList());
3. Collectors.toCollection()用自定义的实现Collection的数据结构收集
List<String> givenList = Arrays.asList("a", "bb", "ccc", "dd");
//用LinkedList收集
List<String> result = givenList.stream().collect(Collectors.toCollection(LinkedList::new));
//用CopyOnWriteArrayList收集
result = givenList.stream().collect(Collectors.toCollection(CopyOnWriteArrayList::new));
//用TreeSet收集
TreeSet<String> collect = givenList.stream().collect(Collectors.toCollection(TreeSet::new));
4. Collectors.toMap()收集成为一个HashMap
需要实现KeyMapper和ValueMapper:
List<String> givenList = Arrays.asList("a", "bb", "ccc", "dd");
//Function.identity()就是a -> a
Map<String, Integer> map1 = givenList.stream().collect(Collectors.toMap(a -> a, String::length));
Map<String, Integer> map2 = givenList.stream().collect(Collectors.toMap(Function.identity(), String::length));
5. Collectors.collectingAndThen()收集之后继续做一些处理
List<String> givenList = Arrays.asList("a", "bb", "ccc", "dd");
//收集后转换为不可变List
ImmutableList<String> collect1 = givenList.stream().collect(Collectors.collectingAndThen(Collectors.toList(), ImmutableList::copyOf));
6. Collectors.joining()对于Stream的字符串拼接
List<String> givenList = Arrays.asList("a", "bb", "ccc", "dd");
System.out.println(givenList.stream().collect(Collectors.joining()));
System.out.println(givenList.stream().collect(Collectors.joining(" | ")));
System.out.println(givenList.stream().collect(Collectors.joining(" | ", "PRE-", "-SUF")));
输出:
abbcccdd
a | bb | ccc | dd
PRE-a | bb | ccc | dd-SUF
7. Collectors.counting()计数
其实没啥必要,Stream就有count()
List<String> givenList = Arrays.asList("a", "bb", "ccc", "dd");
long count = givenList.stream().filter(s -> s.length() > 1).count();
long count2 = givenList.stream().filter(s -> s.length() > 1).collect(Collectors.counting());
8. Collectors.summarizingDouble/Long/Int()生成统计数据
List<String> givenList = Arrays.asList("a", "bb", "ccc", "dd");
System.out.println(givenList.stream().collect(Collectors.summarizingInt(String::length)));
输出:
IntSummaryStatistics{count=4, sum=8, min=1, average=2.000000, max=3}
此外还有获取具体的:
Collectors.averagingDouble/Long/Int()
Collectors.summingDouble/Long/Int()
Collectors.maxBy()/minBy()
Optional<String> result = givenList.stream()
.collect(maxBy(Comparator.naturalOrder()));
9. Collectors.groupingBy()分类成一个Map
Map<Integer, Set<String>> result = givenList.stream().collect(groupingBy(String::length, toSet()));
结果:
{1:["a"],2:["bb","dd"],3:["ccc"]}
10. Collectors.partitioningBy()分类成一个key为True和Flase的Map
Map<Boolean, List<String>> result = givenList.stream().collect(partitioningBy(s -> s.length() > 2))
结果:
{false=["a", "bb", "dd"], true=["ccc"]}