jdk1.8新特性对相关对集合的操作

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<Dish> any = toLists().stream().filter(s -> s.getName().length() > 2).findAny();

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

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

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

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

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

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

7、分组高级Map

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

8List快速转Map

Map<Integer,Apple> 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<Dish> maxDish = Dish.menu.stream().collect(Collectors.maxBy(Comparator.comparing(Dish::getCalories)));maxDish.ifPresent(System.out::println);

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

11、去重

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

12、分组之toMap

Map<Integer,Integer> appleMap = appleList.stream().collect(Collectors.toMap(a -> a.getId(), a -> a.getId(),(k1,k2)->k1));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值