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());
JAVA8之Lambda表达式去重、分组、求和操作
最新推荐文章于 2023-03-22 11:27:16 发布