0
点赞
收藏
分享

微信扫一扫

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())));

8、List快速转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));


举报

相关推荐

0 条评论