整合lambda表达式对List集合进行去重、分组、排序、过滤、求和、最值方法工具类

目录

1.分组

2.去重

3.排序

4.过滤

5.求和


1.分组

//(按时间字段升序) 
 Map<String, List<RecycleIntroDataVo>> collect = dtos.stream()
                .sorted(Comparator.comparing(RecycleIntroDataVo::getRecycleTime).reversed())
                .collect(Collectors.groupingBy(RecycleIntroDataVo::getRecycleNameZHCN, LinkedHashMap::new, Collectors.toList()));

2.去重

list.stream().filter(distinctByKey(City::getCity)).collect(Collectors.toList());

3.排序

//获取对象中的id集合-升序
List<Integer> integerList = detailPageVoList.stream().sorted(Comparator.comparing(ArticleDetailPageVo::getId))
                    .map(ArticleDetailPageVo::getId).collect(Collectors.toList());

//获取对象中某个属性
 List<String> list= cities.stream().map(City :: getCity).collect(Collectors.toList());

4.过滤

//排除掉工号为201901的用户
    List<User> userCommonList = userList.stream().filter(a -> !a.getJobNumber().equals("201901")).collect(Collectors.toList());

5.求和

//最小值
Integer min = list.stream().mapToInt(User::getAge).min().getAsInt();
//最大值
Integer max = list.stream().mapToInt(User::getAge).max().getAsInt();
//平均值
Double average = list.stream().mapToInt(User::getAge).average().getAsDouble();
//和
Integer sum = list.stream().mapToInt(User::getAge).sum();
System.out.println("最小值:"+min+", 最大值"+max+", 平均值:"+average+", 和:"+sum);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 8 的 stream 和 lambda 表达式提供了非常方便的操作 list 的方式,下面是通过 stream 去重的示例代码: ```java List<Integer> list = Arrays.asList(1, 2, 3, 2, 4, 1); List<Integer> distinctList = list.stream().distinct().collect(Collectors.toList()); System.out.println(distinctList); // 输出 [1, 2, 3, 4] ``` 上述代码中,我们首先创建了一个包含重复元素的 list,然后通过 stream() 方法将其转换为 Stream 对象,接着调用 distinct() 方法去重,最后用 collect(Collectors.toList()) 方法将 Stream 转换为 List。 除了去重,stream 还提供了很多其他的操作,如分组过滤求和最值排序等等。下面是一些示例代码: ```java // 分组 Map<Boolean, List<Integer>> groupByEven = list.stream().collect(Collectors.partitioningBy(i -> i % 2 == 0)); System.out.println(groupByEven); // 输出 {false=[1, 3, 1], true=[2, 2, 4]} // 过滤 List<Integer> filteredList = list.stream().filter(i -> i > 2).collect(Collectors.toList()); System.out.println(filteredList); // 输出 [3, 4] // 求和 int sum = list.stream().mapToInt(Integer::intValue).sum(); System.out.println(sum); // 输出 13 // 最值 Optional<Integer> max = list.stream().max(Integer::compare); System.out.println(max.get()); // 输出 4 // 排序 List<Integer> sortedList = list.stream().sorted().collect(Collectors.toList()); System.out.println(sortedList); // 输出 [1, 1, 2, 2, 3, 4] // 去重 List<Integer> distinctList2 = list.stream().distinct().collect(Collectors.toList()); System.out.println(distinctList2); // 输出 [1, 2, 3, 4] ``` 上述代码中,我们使用 partitioningBy() 方法list 分成奇数和偶数两组;使用 filter() 方法过滤出大于 2 的元素;使用 mapToInt() 方法将 Integer 转换为 int,再使用 sum() 方法求和;使用 max() 方法找出最大值;使用 sorted() 方法排序;使用 distinct() 方法去重。注意,使用 distinct() 方法去重时,需要保证元素的类型实现了 equals() 和 hashCode() 方法,否则可能会出现意外的结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值