对象list排序
dictList.sort(Comparator.comparing(Dict::getSort).reversed());
dictList.sort(Comparator.comparing(Dict::getSort));
Comparator<DBFieldInfoTemp> byTableName = Comparator.comparing(DBFieldInfoTemp::getTableName);
Comparator<DBFieldInfoTemp> byLevel = Comparator.comparing(DBFieldInfoTemp::getLevel, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if (o1 != null && o2 != null) {
return o2.compareTo(o1);
} else if (o1 == null && o2 == null) {
return 0;
} else {
return o1 == null ? 1 : -1;
}
}
});
alertFields = JSONArray.parseArray(dbProcessInfo.getAlertFields(), DBFieldInfoTemp.class);
Collections.sort(alertFields, byTableName.thenComparing(byLevel));
对象list分组
Map<String, List<ClassEntity>> collect = classEntities.stream().collect(Collectors.groupingBy(ClassEntity::getGrade));
Map<String, List<MeoEpidemicEmpDeclare>> mapGroup = epidemicEmpDeclareList.stream().collect(Collectors.groupingBy(v -> v.getEmpPhone() + v.getPlaceId() + v.getEmpIdCard()));
- 根据list中每个对象的单个属性进行分组并进行组内排序
TreeMap<Integer, List<StudenData>> listTreeMap = studentList.parallelStream().collect(Collectors.groupingBy( StudentData::getVerSionCount,
() -> new TreeMap<>((Comparator.reverseOrder())),
Collectors.toList()));
对象list去重
List<Player> newList = playerList.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Player::getName))),
ArrayList::new));
List<ClassEntity> distinctClass = classEntities.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(
() -> new TreeSet<>(
Comparator.comparing(
o -> o.getProfessionId() + ";" + o.getGrade()))), ArrayList::new));