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

Jdk8相关对集合的操作

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

Jdk8相关对集合的操作

1、以xx大于3位数的过滤,选择3条数据并输出

toLists().stream().filter(d->d.getName().length()>3).limit(3).distinct().forEach(s-> System.out.println(s));

2、xx中所有xx价格是否全都2000以上

Boolean temp = toLists().stream().allMatch(d->d.getPrice()>2000);

3、找到第一个数据,直接返回

Optional any = toLists().stream().filter(s -> s.getName().length() > 2).findAny();

4、自定义排序,通过Y坐标逆排序

List newList = lineContents.stream().sorted(Comparator.comparingDouble(LineContent::getY).reversed()).collect(Collectors.toList());

5、遍历将Map集合的Value 存放到list集合中

List leftList = leftContentMap.entrySet().stream().map(o-> o.getValue()).collect(Collectors.toList());

6、map分组采用Collectors.groupingBy的样子

Map> resultList = couponList.stream().collect(Collectors.groupingBy(Coupon::getCouponId));

7、分组高级Map

> resultList = couponList.stream().collect(Collectors.groupingBy(Coupon::getCouponId,Collectors.mapping(Coupon::getName,Collectors.toList())));

8、List快速转Map

Map appleMap = appleList.stream().collect(Collectors.toMap(Apple::getId, a -> a,(k1,k2)->k1));

9、数据求和

BigDecimal totalMoney = appleList.stream().map(Apple::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);

10、查找流中最大 最小值

Optional maxDish = Dish.menu.stream().collect(Collectors.maxBy(Comparator.comparing(Dish::getCalories)));maxDish.ifPresent(System.out::println);

Optional minDish = Dish.menu.stream(). collect(Collectors.minBy(Comparator.comparing(Dish::getCalories)));minDish.ifPresent(System.out::println);

11、去重

List unique = appleList.stream().collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparingLong(Apple::getId))),ArrayList::new));

12、分组之toMap

Map appleMap = appleList.stream().collect(Collectors.toMap(a -> a.getId(), a -> a.getId(),(k1,k2)->k1));

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

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

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