一:简介
Stream中有两个个方法collect和collectingAndThen用于对流中的数据进行处理,可以对流中的数据进行聚合操作,如:
- 将流中的数据转成集合类型: toList、toSet、toMap、toCollection
- 将流中的数据(字符串)使用分隔符拼接在一起:joining
- 对流中的数据求最大值maxBy、最小值minBy、求和summingInt、求平均值averagingDouble
- 对流中的数据进行映射处理 mapping
- 对流中的数据分组:groupingBy、partitioningBy
- 对流中的数据累计计算:reducing
![c6e8c7c8eeacda26bed48f50ba11e5b4.png](https://img-blog.csdnimg.cn/img_convert/c6e8c7c8eeacda26bed48f50ba11e5b4.png)
public final class Collectors { // 转换成集合 public static Collector> toList(); public static Collector> toSet(); public static Collector> toMap(Function super T, ? extends K> keyMapper, Function super T, ? extends U> valueMapper); public static > Collector toCollection(Supplier collectionFactory); // 拼接字符串,有多个重载方法 public static Collector joining(CharSequence delimiter); public static Collector joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix); // 最大值、最小值、求和、平均值 public static Collector> maxBy(Comparator super T> comparator); public static Collector> minBy(Comparator super T> comparator); public static Collector summingInt(ToIntFunction super T> mapper); public static Collector averagingDouble(ToDoubleFunction super T> mapper); // 分组:可以分成true和false两组,也可以根据字段分成多组