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

// 将list转为map(并解决重复key问题) public static void Test(List<TestVO> list) { Map<Long, Integer> map1 = list.stream().collect(Collectors.toMap(TestVO::getId, TestVO::getAge, (v1, v2) -> v1)); System.out.println("转为map的数据为" + map1); }

根据指定条件过滤

//过滤指定查询条件(查询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, "王大朋

  • 21
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值