Java 8 提供了Stream流进行数据处理;流只能遍历一次
list.stream().filter(item -> Objects.equals("1", item.getId())).collect(Collectors.toList());
list.stream().distinct().collect(Collectors.toList());
list.stream().limit(2).collect(Collectors.toList());
list.stream().skip(2).collect(Collectors.toList());
list.stream().sorted(Comparator.comparing(OperaLogDto ::getId,Comparator.comparingInt(Integer ::parseInt))).collect(Collectors.toList());
list.stream().sorted(Comparator.comparing(OperaLogDto :: getId).reversed()).collect(Collectors.toList());
list.stream().map(OperaLogDto :: getUserName).collect(Collectors.toList());
list.stream().map(OperaLogDto :: getUserName).map(String :: length).collect(Collectors.toList());
list.stream().filter(item -> "test".equals(item.getUserName())).map(item -> item.getUserName().split("//|")).flatMap(Arrays :: stream).collect(Collectors.toList());
list.stream().anyMatch(item -> item.getId().equals("1"));
Optional<OperaLogDto> first = list.stream().filter(item -> item.getId().equals("1")).findFirst();
list.stream().map(item -> new BigDecimal(item.getId())).reduce(BigDecimal.ZERO,BigDecimal::add).toPlainString();
list.stream().max(Comparator.comparing(OperaLogDto::getId));
Map<String, List<OperaLogDto>> collect = list.stream().collect(Collectors.groupingBy(OperaLogDto::getUserName));
for (String key : collect.keySet()){
List<OperaLogDto> operaLogDtos = collect.get(key);
}
String collect2 = list.stream().map(item -> item.getId().split(""))
.flatMap(Arrays::stream).collect(Collectors.joining());
Map<Integer, List<OperaLogDto>> collect = list.stream().collect(Collectors.groupingBy(item -> {
if (item.getId().equals("1")) {
return 1;
} else {
return 2;
}
}));
Map<Integer, Long> collect1 = list.stream().collect(Collectors.groupingBy(item -> {
if (item.getId().equals("1")) {
return 1;
} else {
return 2;
}
}, Collectors.counting()));
Map<Boolean, List<OperaLogDto>> collect3 = list.stream().collect(Collectors.partitioningBy(OperaLogDto::getSave));
Map<Boolean, Map<String, List<OperaLogDto>>> collect3 = list.stream().collect(Collectors.partitioningBy(OperaLogDto::getSave, Collectors.groupingBy(OperaLogDto::getUserName)));
上面的方法也可进行复合使用,也可以使用流来进行跨包的转换
list.stream().map(OperaLogDto :: convertTo).collect(Collectors.toList());
在OperaLogDto 中转换即可
public static OperaLogDto convertTo(OperaLogTo dto) {
return new OperaLogDto();
}
|