今天码程序想给一个list去个重,脑子里闪过用set过滤下,或者用list.contains()判断循环下,最后我用的是java8,哈哈哈!
很骄傲,可以说是很喜欢了
resultList.stream().distinct().collect(Collectors.toList());
好久之后看到这,也还是挺惊喜的,当时这个居然都要写这里, list转map,
Function.identity()是代表本身的意思
List<TaskDto> csvTaskDtos = new ArrayList<>();
Map<String, TaskDto> csvTaskDtoMap = csvTaskDtos.stream().collect(Collectors.toMap(TaskDto::getThemeJstId, Function.identity()));
两个list取并集 各自单独的独有属性
Set<String> systemTaskDtoMapKey = systemTaskDtoMap.keySet(); Set<String> csvTaskDtoMapKey = csvTaskDtoMap.keySet(); Set<String> includeBothKey = systemTaskDtoMapKey.stream().filter(k -> csvTaskDtoMapKey.contains(k)).collect(Collectors.toSet()); Set<String> includeSystemKey = systemTaskDtoMapKey.stream().filter(k -> !csvTaskDtoMapKey.contains(k)).collect(Collectors.toSet()); Set<String> includeCsvKey = csvTaskDtoMapKey.stream().filter(k -> !systemTaskDtoMapKey.contains(k)).collect(Collectors.toSet());