// 根據UserName去重 List<UserEntity> distinct2 = userEntities.stream().collect( Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(UserEntity::getUserName))), ArrayList::new) ); // 根據UserName + Worknum 去重 List<UserEntity> distinct3 = userEntities.stream().collect( Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(e-> e.getUserName() + ";" + e.getWorknum()))), ArrayList::new) );