一、对两个list中的对象中的某个属性值进行对比,之后操作相同属性的两个对象
方法一
//将需要对比的值单独拎出来放到map中,其中就是将RealNameUploadRecord中queryStatusToken作为key
Map<String, RealNameUploadRecord> collect1 = list.stream().collect(Collectors.toMap(RealNameUploadRecord::getQueryStatusToken, n -> n));
//之后对另一个list进行循环并用map的键值对获取这个属性,有就代表匹配上了
list1.forEach(n -> {
if (collect1.containsKey(n.getQueryStatusToken())) {
RealNameUploadRecord realNameUploadRecord = collect1.get(n.getQueryStatusToken());
n.setUpdateTime(LocalDateTime.now());
}
});
方法二
//直接使用filter进行过滤之后peek
List<RealNameUploadRecord> result = list.stream()
.flatMap(x -> list1.stream()
.filter(y -> x.getQueryStatusToken().equals(y.getQueryStatusToken()) )
.peek(y -> {
y.setUpdateTime(LocalDateTime.now());
}))
.collect(Collectors.toList());
二、对list属性去重
方法一
//直接去重
List<RealNameUploadRecord> collect =
clockingVO.stream().distinct().collect(Collectors.toList());
方法二
//根据ProjectName与ProjectCode两个属性的值去重,因为两个都是string所以能行
List list1 =
clockingVO.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(n->n.getProjectName()+n.getProjectCode()))), ArrayList::new));
//根据单个属性去重
List list1 =
clockingVO.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()->new TreeSet<>(Comparator.comparing(RealNameUploadRecord::getQueryStatusToken))), ArrayList::new));
list获取对象中指定的字符串属性,并根据逗号分割成list
List<String> collect1 = list.parallelStream().map(NotifyAnnounce::getAnnounceIds)
.filter(StringUtils::isNotBlank).map(n -> Arrays.asList(n.split(",")))
.flatMap(Collection::parallelStream).distinct().collect(Collectors.toList());
list获取下标转换成map,记录每个对象所处下标
ArrayList<String> stringArrayList = new ArrayList<>();
stringArrayList.add("a");
stringArrayList.add("b");
stringArrayList.add("b");
stringArrayList.add("a");
//很明显直接获取下标无法使用
List<Integer> collect = stringArrayList.stream().map(stringArrayList::indexOf).collect(Collectors.toList());
System.out.println(collect);
//游标方式获取
Map<String, List<Integer>> collect1 = IntStream.range(0, stringArrayList.size()).parallel().boxed()
.collect(Collectors.toMap(stringArrayList::get, Collections::singletonList, (key1, key2) -> {
List<Integer> bt= new ArrayList<>(key1);
bt.addAll(key2);
return bt;
}));
System.out.println(collect1);
获取某个时间段每天的日期
LocalDateTime min = LocalDateTime.now().with(LocalTime.MIN);
LocalDateTime max = LocalDateTime.now().plusDays(10).with(LocalTime.MAX);
List<LocalDateTime> collect = Stream.iterate(min, localDate -> localDate.plusDays(1))
.limit(ChronoUnit.DAYS.between(min, max) + 1)
.collect(Collectors.toList());