[Java Basics] List in java deduplicates a certain field and groups a certain field

When there is a certain field in a List or several of them are the same, such data is regarded as repeated data, and the repeated data needs to be removed.

  1. According to a certain field deduplication, you can also use multiple fields to deduplication
List<Entity> arrays = arr.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()
						-> new TreeSet<>(Comparator.comparing(Entity :: getId))), ArrayList::new));

2. Group a field

Map<Long, List<entity>> map = arrays.stream().collect(Collectors.groupingBy(entity::getGrade));

Guess you like

Origin blog.csdn.net/someday____/article/details/130523995