JDK1.8新特性(二):Collectors收集器类

JDK1.8新特性(二):Collectors收集器类一 什么是 Collectors Java 8 API 添加了一个新的抽象称为流 Stream 我们借助 Stream API 可以很方便的操作流对象 Stream 中有两个方法 collect 和 collectingAn 可以借助 Collectors 收集器类对流中的数据进行聚合操作 例如将素累积到集合中 并根据各种标准对素进行汇总 分类等操作 二 举个例子 获取 String 集合

一. 什么是Collectors?

Java 8 API添加了一个新的抽象称为流Stream,我们借助Stream API可以很方便的操作流对象。

Stream中有两个方法collect和collectingAndThen,可以借助Collectors收集器类对流中的数据进行聚合操作,例如将元素累积到集合中,并根据各种标准对元素进行汇总,分类等操作。

二. 举个例子?

//获取String集合
List strings = Arrays.asList("ab", "", "bc", "cd", "abcd","", "jkl");
//通过stream操作集合
List stringList = strings.stream()
//为集合中的每一个元素拼接“???”
.map(s -> s += "???")
//返回集合
.collect(Collectors.toList());

如代码所示,我们可以很方便的通过Collectors类对被处理的流数据进行聚合操作,包括并不仅限与将处理过的流转换成集合

三. 如何使用Collectors?

1. Collectors类中提供的方法

总结一下,就是以下几类方法:

1.1 转换成集合:toList(),toSet(),toMap(),toCollection()

1.2 将集合拆分拼接成字符串:joining()

1.3 求最大值、最小值、求和、平均值 :maxBy(),minBy(),summingInt(),averagingDouble()

1.4 对集合分组:groupingBy(),partitioningBy()

1.5 对数据进行映射:mapping()

2. Collectors类方法源码

public final class Collectors {

// 转换成集合
public static Collector> toList();
public static Collector> toSet();
public static Collector> toMap(Function keyMapper,
Function 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 comparator);
public static Collector> minBy(Comparator comparator);
public static Collector summingInt(ToIntFunction mapper);
public static Collector averagingDouble(ToDoubleFunction mapper);

// 分组:可以分成true和false两组,也可以根据字段分成多组
public static Collector>> groupingBy(Function classifier);
// 只能分成true和false两组
public static Collector>> partitioningBy(Predicate predicate);

// 映射
public static Collector mapping(Function mapper,
Collector downstream);

public static Collector reducing(U identity,
Function mapper,
BinaryOperator op);
}

四. 实例

//接下来的示例代码基于此集合
List strings = Arrays.asList("ab", "s", "bc", "cd", "abcd","sd", "jkl");

1. 将流数据转换成集合

//转换成list集合
List stringList = strings.stream().collect(Collectors.toList());

//转换成Set集合
Set stringSet = strings.stream().collect(Collectors.toSet());

//转换成Map集合
Map stringObjectMap = strings.stream()
.collect(Collectors.toMap(k -> k, v -> v ));

System.out.println(stringList);
System.out.println(stringSet);
System.out.println(stringObjectMap);

//=================打印结果=================
[ab, s, bc, cd, abcd, sd, jkl]
[ab, bc, cd, sd, s, jkl, abcd]
{sd=sd, cd=cd, bc=bc, ab=ab, s=s, jkl=jkl, abcd=abcd}

2. 将集合拆分拼接成字符串

//joining
String str1 = strings.stream()
.collect(Collectors.joining("--"));

//collectingAndThen
String str2 = strings.stream()
.collect(Collectors.collectingAndThen(
//在第一个joining操作的结果基础上再进行一次操作
Collectors.joining("--"), s1 -> s1 += ",then"
));

System.out.println(str1);
System.out.println(str2);

//=================打印结果=================
ab--s--bc--cd--abcd--sd--jkl
ab--s--bc--cd--abcd--sd--jkl,then

3. 求最大值、最小值、求和、平均值

List list = Arrays.asList(1, 2, 3, 4, 5);

//最大值
Integer maxValue = list.stream().collect(Collectors.collectingAndThen(
//maxBy需要Comparator.comparingInt来确定排序规则
Collectors.maxBy(Comparator.comparingInt(a -> a)), Optional::get
));
//最小值
Integer minValue = list.stream().collect(Collectors.collectingAndThen(
//minBy需要Comparator.comparingInt来确定排序规则
Collectors.minBy(Comparator.comparingInt(a -> a)), Optional::get
));
//求和
Integer sumValue = list.stream().collect(Collectors.summingInt(i -> i));
//平均值
Double avgValue = list.stream().collect(Collectors.averagingDouble(i -> i));

System.out.println("列表中最大的数 : " + maxValue);
System.out.println("列表中最小的数 : " + minValue);
System.out.println("所有数之和 : " + sumValue);
System.out.println("平均数 : " + avgValue);

//=================打印结果=================
列表中最大的数 : 5
列表中最小的数 : 1
所有数之和 : 15
平均数 : 3.0

虽然这样也可以,但是明显IntSummaryStatistics要更灵活点

4. 对集合分组

Map> map = strings.stream()
//根据字符串长度分组(同理,对对象可以通过某个属性分组)
.collect(Collectors.groupingBy(String::length));

Map> map2 = strings.stream()
//根据字符串是否大于2分组
.collect(Collectors.groupingBy(s -> s.length() > 2));

System.out.println(map);
System.out.println(map2);

//=================打印结果=================
{1=[s], 2=[ab, bc, cd, sd], 3=[jkl], 4=[abcd]}
{false=[ab, s, bc, cd, sd], true=[abcd, jkl]}

5.对数据进行映射

String str = strings.stream().collect(Collectors.mapping(
//先对集合中的每一个元素进行映射操作
s -> s += ",mapping",
//再对映射的结果使用Collectors操作
Collectors.collectingAndThen(Collectors.joining(";"), s -> s += "=====then" )
));

System.out.println(str);

//=================打印结果=================
ab,mapping;s,mapping;bc,mapping;cd,mapping;abcd,mapping;sd,mapping;jkl,mapping=====then
编程小号
上一篇 2025-03-14 21:46
下一篇 2025-04-11 21:30

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/124166.html