java8 集合_java8 集合神操作

///根据name分组

Map> collect =listUser.stream().collect(Collectors.groupingBy(UserInfo::getName));

System.out.println(JSON.toJSONString(collect));///Collectors.averagingDouble 取出平均值

Optional.ofNullable(list.stream().collect(Collectors.averagingDouble(Dish::getOalories)))

.ifPresent(System.out::println);///collectingAndThen 对结果进行处理

Optional.ofNullable(list.stream().collect(Collectors.collectingAndThen(Collectors.averagingDouble(Dish::getOalories),(a->"平均值:"+a))))

.ifPresent(System.out::println);

List dishList = list.stream().filter(d ->d.getType().equals(Dish.Type.OTHER)).collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));//dishList.add(new Dish("salmon", false, 550, Dish.Type.FISH));

System.out.println(JSON.toJSONString(dishList));///打印集合个数

Optional.ofNullable(list.stream().collect(Collectors.counting())).ifPresent(System.out::println);///{OTHER=4, MEAT=3, FISH=2} 分组之后统计分组的个数

Optional.ofNullable(list.stream().collect(Collectors.groupingBy(Dish::getType,Collectors.counting()))).ifPresent(System.out::println);///分组之后 求出平均值 并且返回的TreeMap

Optional.ofNullable(list.stream().collect(Collectors.groupingBy(Dish::getType, TreeMap::new,Collectors.averagingDouble(Dish::getOalories)))).ifPresent(System.out::println);///DoubleSummaryStatistics 统计集合的值 DoubleSummaryStatistics{count=9, sum=4200.000000, min=120.000000, average=466.666667, max=800.000000}

DoubleSummaryStatistics summaryStatistics =list.stream().collect(Collectors.summarizingDouble(Dish::getOalories));

System.out.println(summaryStatistics.toString());///concurrentMap 和 Map使用一样

ConcurrentMap> collect1 =list.stream().collect(Collectors.groupingByConcurrent(Dish::getType));

System.out.println(collect1);///转换为skipListMap

ConcurrentSkipListMap collect2 = list.stream().collect(Collectors.groupingByConcurrent(Dish::getType, ConcurrentSkipListMap::new, Collectors.averagingDouble(Dish::getOalories)));

String collect3= list.stream().collect(Collectors.mapping(Dish::getName, Collectors.joining(",", "[", "]")));

System.out.println(collect3);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值