Java中给List<T> 对象集合去重
Java中给List 对象集合去重
List<Student> getStudentList = studentMapper.getStudentList();List<Student> distinctInsurance = distinctByField(getStudentList, Student::getCertNo);
public static <T> List<T> distinctByField(List<T> list, Function<T, Object> fieldExtractor) {Set<Object> seen = new HashSet<>();return list.stream().filter(item -> seen.add(fieldExtractor.apply(item))).collect(Collectors.toList());}