jdk8 lambda表达式list操作分组、过滤、求和、最值、排序、去重(1)

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

//过滤指定查询条件(查询age>15并且age<18的数据) public static void Test1(List<TestVO> list) { List<TestVO> collect = list.stream().filter((e) -> e.getAge() > 15 && e.getAge() < 18 ).collect(Collectors.toList()); System.out.println("过滤后的数据为"); TestVO.printStudents(collect); }

根据指定列分组

public static void Test2(List<TestVO> list) { Map<Integer, List<TestVO>> map = list.stream().collect(Collectors.groupingBy(TestVO::getAge)); System.out.println("根据指定列分组" + map); }

通过map获取指定列集合

public static void Test3(List<TestVO> list) { List<Integer> ageList = list.stream() .map(TestVO::getAge) .collect(Collectors.toList()); System.out.println("通过map获取指定年龄列集合" + ageList); }

根据 List 中 Object 某个属性去重

public static void Test4() { //测试数据,请不要纠结数据的严谨性 List<TestVO> list = new ArrayList<>(); list.add(new TestVO(1, "李小明", 18)); list.add(new TestVO(2, "李小明", 19)); list.add(new TestVO(3, "王大朋", 16)); list.add(new TestVO(4, "陈小跑", 10)); // 这里我们引入了两个静态方法,以及通过 TreeSet<> 来达到获取不同元素的效果 // 1. import static java.util.stream.Collectors.collectingAndThen; // 2. import static java.util.stream.Collectors.toCollection; ArrayList<TestVO> collect = list.stream().collect(collectingAndThen(toCollection(() -> new TreeSet<>(Comparator.comparing(TestVO::getName))), ArrayList::new)); TestVO.printStudents(collect); /* ---------------------------------------------------------- TestVO(id=1, name=李小明, age=18) TestVO(id=3, name=王大朋, age=16) TestVO(id=4, name=陈小跑, age=10)*/ }

//list统计(求和、最大、最小、平均)

public static void Test5() { //测试数据,请不要纠结数据的严谨性 List<TestVO> list = new ArrayList<>(); list.add(new TestVO(1, "李小明", 18)); list.add(new TestVO(2, "李小明", 19)); list.add(new TestVO(3, "王大朋", 16)); list.add(new TestVO(4, "陈小跑", 10)); double sum = list.stream().mapToDouble(TestVO::getAge).sum();//和 OptionalDouble max = list.stream().mapToDouble(TestVO::getAge).max();//最大 OptionalDouble min = list.stream().mapToDouble(TestVO::getAge).min();//最小 OptionalDouble average = list.stream().mapToDouble(TestVO::getAge).average();//平均值 System.out.println("和" + sum); System.out.println("最大" + max.getAsDouble()); System.out.println("最小" + min.getAsDouble()); System.out.println("平均值" + average.getAsDouble()); /* 和63.0 最大19.0 最小10.0 平均值15.75*/ }

最后

由于篇幅原因,就不多做展示了
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
示了
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值