栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java8 Lambda Stream collect Collectors 常用详细实例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java8 Lambda Stream collect Collectors 常用详细实例


查找Integer类型集合的平均值。Integer类型集合的平均值:averagingInt()

注意:返回的是Double类型而不是 int类型

List integerList = Arrays.asList(1,1,2,3,4,5,6,6);
Double d = integerList.stream().collect(Collectors.averagingInt(x -> x*2));

Double类型集合的平均值:averagingDouble()

查找Double类型集合的平均值。

List doubleList = Arrays.asList(1.1,2.2,3.3,4.4,5.5);
Double d = doubleList.stream().collect(Collectors.averagingDouble(x -> x));

创建Map:toMap()

根据集合的值创建Map。

List stringList = Arrays.asList("aaaa","bbbbb","ccccc");
Map map = stringList.stream().collect(Collectors.toMap(Function.identity(),String::length));

创建了一个Map,其中集合值作为key,在集合中的出现次数作为值。

在创建map时处理列表的重复项
集合中可以包含重复的值,因此,如果想从列表中创建一个Map,并希望使用集合值作为map的key,那么需要解析重复的key。由于map只包含唯一的key,可以使用比较器来实现这一点。

List stringList = Arrays.asList("aaaa","bbbbb","ccccc","aaaa");
Map map = stringList.stream().collect(Collectors.toMap(Function.identity(),String::length,(i1,i2) -> i1));

Function.identity()指向集合中的值,i1和i2是重复键的值。可以只保留一个值,这里选择i1,也可以用这两个值来计算任何东西,比如把它们相加,比较和选择较大的那个,等等。

整数求和:summingInt ()

查找集合中所有整数的和。它并不总是初始集合的和,就像我们在下面的例子中使用的是字符串列表,首先我们把每个字符串转换成一个等于它的长度的整数,然后把所有的长度相加。

List stringList = Arrays.asList("aaaa","bbbbb","ccccc");
Integer collect4 = stringList.stream().collect(Collectors.summingInt(String::length));

或直接集合值和

List integerList = Arrays.asList(1,1,2,3,4,5,6,6);
Integer sum = integerList.stream().collect(Collectors.summingInt(x -> x));

类似于整数求和,只是它用于双精度值

List  doubleList = Arrays.asList(1.1,2.2,3.3,4.4,5.5);
Double sum = doubleList.stream().collect(Collectors.summingDouble(x ->x));

Long求和:summingLong ()

与前两个相同,用于添加long值或int值。可以对int值使用summinglong(),但不能对long值使用summingInt()。

List longList = Arrays.asList(100l,200l,300l);
Long sum = longList.stream().collect(Collectors.summingLong(x ->x));
 返回List集合: toList()

用于将元素累积到List集合中。它将创建一个新List集合(在不更改当前集合的情况下)。

List integerList = Arrays.asList(1,2,3,4,5,6);
integerList.stream().map(x -> x*x).collect(Collectors.toList());

返回Set集合: toSet()

用于将元素累积到Set集合中。会删除重复元素。

List integerList = Arrays.asList(1,1,2,3,4,5,6,6);
integerList.stream().map(x -> x*x).collect(Collectors.toSet());

返回指定的集合: toCollection()

可以将元素累积到指定的集合中。

List integerList= Arrays.asList(1,1,2,3,4,5,6,6);
integerList.stream().filter(x -> x >2).collect(Collectors.toCollection(linkedList::new));

计算元素数量: Counting()

用于返回计算集合中存在的元素个数。

List integerList = Arrays.asList(1,1,2,3,4,5,6,6);
Long collect = integerList.stream().filter(x -> x <4).collect(Collectors.counting());

求最小值: minBy()

用于返回列表中存在的最小值。

List integerList = Arrays.asList(1,1,2,3,4,5,6,6);
List stringList = Arrays.asList("aaaa","bbbbb","ccccc");
integerList.stream().collect(Collectors.minBy(Comparator.naturalOrder())).get();
stringList.stream().collect(Collectors.minBy(Comparator.naturalOrder())).get();

按照整数排序返回1,按照字符串排序返回alpha

可以使用reverseOrder()方法反转顺序。

List integerList= Arrays.asList(1,1,2,3,4,5,6,6);
integerList.stream().collect(Collectors.minBy(Comparator.reverseOrder())).get();

同时可以自定义的对象定制比较器。

返回不可修改的List集合:toUnmodifiableList()

用于创建只读List集合。任何试图对此不可修改List集合进行更改的尝试都将导致UnsupportedOperationException。

List stringList = Arrays.asList("aaaa","bbbbb","ccccc");
List collects = stringList.stream().collect(Collectors.toUnmodifiableList());

返回不可修改的Set集合:toUnmodifiableSet()

用于创建只读Set集合。任何试图对此不可修改Set集合进行更改的尝试都将导致UnsupportedOperationException。它会删除重复元素。

List stringList = Arrays.asList("aaaa","bbbbb","ccccc","aaaa");
Set readonlySet = stringList.stream().sorted().collect(Collectors.toUnmodifiableSet());

Long类型集合的平均值:averagingLong()

查找Long类型集合的平均值。

注意:返回的是Double类型而不是 Long类型

List longList = Arrays.asList(100l,200l,300l);

Double d = longList.stream().collect(Collectors.averagingLong(x -> x * 2));

汇总整数:summarizingInt ()

它给出集合中出现的值的所有主要算术运算值,如所有值的平均值、最小值、最大值、所有值的计数和总和。可以使用get方法提取不同的值

List integerList = Arrays.asList(1,1,2,3,4,5,6,6);
IntSummaryStatistics stats = integerList.stream().collect(Collectors.summarizingInt(x -> x ));

//单个取值示例
//stats.getAverage();  

// stats.getMax();      

// stats.getMin();      

// stats.getCount();    

// stats.getSum();      

分组函数:GroupingBy ()

GroupingBy()是一种高级方法,用于从任何其他集合创建Map。

List stringList = Arrays.asList("aaaa","bbbbb","ccccc");
Map> collect = stringList.stream().collect(Collectors.groupingBy(String::length));

它将字符串长度作为key,并将该长度的字符串列表作为value。

List stringList = Arrays.asList("aaaa","bbbbb","ccccc");
Map> collect1 = stringList.stream().collect(Collectors.groupingBy(String::length,Collectors.toCollection(linkedList::new)));

这里指定了Map中需要的列表类型(Libkedlist)。

交集 (list1 + list2)
List intersect = list1.stream()
                         .filter(list2::contains)
                         .collect(Collectors.toList());
差集
//(list1 - list2)
List reduce1 = list1.stream().filter(item -> !list2.contains(item)).collect(toList());
//(list2 - list1)
List reduce2 = list2.stream().filter(item -> !list1.contains(item)).collect(toList());
并集
//使用并行流 
List listAll = list1.parallelStream().collect(toList());
List listAll2 = list2.parallelStream().collect(toList());
listAll.addAll(listAll2);
去重并集
List listAllDistinct = listAll.stream()
.distinct().collect(toList());
从List中过滤出一个元素
User match = Future home of users.stream()
.filter((user) -> user.getId() == 1).findAny().get();

欢迎大家多多指教 ,补充

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/590997.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号