1、list单个数据+1
list.stream().forEach(x->{x.age+=1;});
list = list.stream().map(s -> s.setAge(s.getAge() + 1)).collect(Collectors.toList());
2、将某个参数取出重组list
List<Integer> ids = list.stream().map(LcsReportformGroupEntity::getId).collect(Collectors.toList());
String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", ")); System.out.println("合并字符串: " + mergedString);
可用map 的 x -> { return value}重新定义一个对象
3、过滤掉不符合条件的数据
//查找身高在1.8米及以上的男生
List<StudentInfo> boys = studentList.stream().filter(s->s.getGender().equals("boy") && s.getHeight() >= 1.8).collect(Collectors.toList());
4、按某个字段排序升序
List<TotalBytesHostBean> result = totalList.stream().sorted(Comparator.comparingLong(TotalBytesHostBean::getTotalBytes)).collect(Collectors.toList());
倒序(降序)
List<TotalBytesHostBean> result = totalList.stream().sorted(Comparator.comparingLong(TotalBytesHostBean::getTotalBytes).reversed()).collect(Collectors.toList());
多个字段排序
List<userInfo> userList3 = userList.stream() .sorted(Comparator.comparing(userInfo::getAge).thenComparing(userInfo::getMoney)) .collect(Collectors.toList());
多字段正序倒序
List<userInfo> userList3 = userList.stream() .sorted(Comparator.comparing(userInfo::getAge).thenComparing(userInfo::getMoney,Comparator.reverseOrder())) .collect(Collectors.toList());
5、LIst 转Map或Map转List /List 中某个字段相同的,另一个字段求和
List<TotalBytesHostBean> result = totalList.stream().collect(Collectors.toMap(TotalBytesHostBean::getHost,
TotalBytesHostBean::getTotalBytes, (x, y) -> x + y))
.entrySet()
.stream()
.map(e -> new TotalBytesHostBean(e.getKey(),e.getValue()))
.sorted(Comparator.comparingLong(TotalBytesHostBean::getTotalBytes)).limit(10).collect(Collectors.toList());
可能有更好的写法、暂时没想到
持续更新中。。。