Lambda表达式最佳实践(3)Collector

2021-04-12 14:56:15 浏览数 (1)

在上一节Stream中提到了Collect方法其实已经说了很多关于Collector的事情,我们这里总结下Collector的一些用法。

Collector一般用于Stream工作流的最后一步的收集。

一些常用的方法

1. Collectors.toList()用ArrayList收集

代码语言:javascript复制
List<String> givenList = Arrays.asList("a", "bb", "ccc", "dd");
List<String> result = givenList.stream().collect(toList());

2. Collectors.toSet()用HashSet收集

代码语言:javascript复制
List<String> givenList = Arrays.asList("a", "bb", "ccc", "dd");
List<String> result = givenList.stream().collect(toList());

3. Collectors.toCollection()用自定义的实现Collection的数据结构收集

代码语言:javascript复制
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:

代码语言:javascript复制
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()收集之后继续做一些处理

代码语言:javascript复制
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的字符串拼接

代码语言:javascript复制
List 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")));

输出:

代码语言:javascript复制
abbcccdd
a | bb | ccc | dd
PRE-a | bb | ccc | dd-SUF

7. Collectors.counting()计数 其实没啥必要,Stream就有count()

代码语言:javascript复制
List 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()生成统计数据

代码语言:javascript复制
List givenList = Arrays.asList("a", "bb", "ccc", "dd");
System.out.println(givenList.stream().collect(Collectors.summarizingInt(String::length)));

输出:

代码语言:javascript复制
IntSummaryStatistics{count=4, sum=8, min=1, average=2.000000, max=3}

此外还有获取具体的: Collectors.averagingDouble/Long/Int() Collectors.summingDouble/Long/Int() Collectors.maxBy()/minBy()

代码语言:javascript复制
Optional result = givenList.stream()
  .collect(maxBy(Comparator.naturalOrder()));

9. Collectors.groupingBy()分类成一个Map

代码语言:javascript复制
Map<Integer, Set<String>> result = givenList.stream().collect(groupingBy(String::length, toSet()));

结果:

代码语言:javascript复制
{1:["a"],2:["bb","dd"],3:["ccc"]}

10. Collectors.partitioningBy()分类成一个key为True和Flase的Map

代码语言:javascript复制
Map<Boolean, List<String>> result = givenList.stream().collect(partitioningBy(s -> s.length() > 2))

结果:

代码语言:javascript复制
{false=["a", "bb", "dd"], true=["ccc"]}

0 人点赞