List<Object>去重方法一:
private List<Account> removeDupById(List<Account> accs) {
Set<Account> accSet = new TreeSet<>(Comparator.comparing(Account::getId));
accSet.addAll(accs);
return new ArrayList<>(accSet);
}
List<Object>去重方法二:
ArrayList<Account> distinctById = list.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(
Comparator.comparing(Account::getId))), ArrayList::new));
字符串類型去重
List<String> strings = list.stream().distinct().collect(Collectors.toList());