JAVA8之Lambda表达式去重、分组、求和操作

1.List<T>去重 
//单个属性
List<UserParam> result = userList.stream().collect(Collectors.collectingAndThen(
        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(UserParam::getOrgCode))), ArrayList::new));
//多属性去重
List newList = list.stream().collect(
        Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(
                        o ->    o.getProductName() + ";" +
                                o.getManufactureName() +";"+
                                o.getShopSign() +";"+
                                o.getSpecComment() +";"+
                                o.getProductTypeCode() +";"+
                                o.getWeight() +";"+
                                o.getWarehouseCode() +";"+
                                o.getPackCode()
                        ))
                ), ArrayList::new));
1-1.List<Map<String,Object>>去重
 List<Map<String, Object>> list2 = list1.stream().collect(
               Collectors.collectingAndThen(
                       Collectors.toCollection(
                               () ->new TreeSet<>(Comparator.comparing(m->m.get("value").toString()))
                       ),ArrayList::new
               )
       )
2.筛选,过滤
result = userList.stream().filter(ls->ls.getId().equals("222")).collect(Collectors.toList());
3.获取最大值,最小值,求和
List<Integer> num = new ArrayList<Integer>();
Integer max = num.stream().reduce(Integer::max).get();//得到最大值
Integer min = num.stream().reduce(Integer::min).get();//得到最小值
Integer sum = num.stream().reduce(0,(a, b) -> a + b);//得到求和数据
4.list转map
Map result1 = list.stream().collect(Collectors.toMap(对象::属性1, 对象::属性2));
5.map转list
map.entrySet().stream().map(e -> new Person(e.getKey(),e.getValue())).collect(Collectors.toList());
6.遍历map
map.forEach((k, v) -> System.out.println("key:value = " + k + ":" + v));
7.分组
//根据多个属性分组
Map<String, List<String>> groupBy = voList.stream().collect(Collectors.groupingBy(CountDefaultOrderVo::getProviderCode,
                    Collectors.mapping(CountDefaultOrderVo::getPackCode, Collectors.toList())));
//根据某一个属性分组                 
Map<Integer, List<TestStreamModel>> map = list.stream().collect(Collectors.groupingBy(t -> t.getGrade()));  
8.获取list集合中单个属性作为一个集合
List<String> courseIds=  users.stream().map(UserEntity::getUserName).collect(Collectors.toList());
9.获取list集合中多个属性组成一个新的list集合
List<AppNameAddrDTO> collect = applicationList.stream().map(item -> AppNameAddrDTO.builder()
    .appName(item.getAppName())
    .appAddress(item.getAppAddress()).build()).collect(Collectors.toList());
10.两个list比对,取出交集
List<PersonCommon> commonList= Person1.stream().filter(item -> item.getName.equals(person2.getName())).collect(Collectors.toList());
11.两个list比对,取出差集
List<PersonCommon> commonList= Person1.stream().filter(item -> !item.getName.equals(person2.getName())).collect(Collectors.toList());
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值