If your project uses java8, there is a very simple way to handle the de-weighting of a list based on the value of an attribute, using the lamba function:
List<UcShopCourseBizPojo> unique = ucShopCourseBizPojoList.stream().collect(
collectingAndThen(
toCollection(() -> new TreeSet<>(comparingLong(UcShopCourseBizPojo::getId))), ArrayList::new));
Copy the code
The poJO UcShopCourseBizPojo (” id “, “id”, “id”, “id”, “id”, “id”, “id”, “id”, “id”, “id”, “id”, “id”);
List<UcShopCourseBizPojo> sLists = shopCourseLists.stream().collect(
Collectors.collectingAndThen(Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(c -> c.getShopId()))), ArrayList::new)
);
Copy the code
However, this method has a disadvantage, that is, the obtained list may return random results each time. For example, to obtain the list of store courses and duplicate the courses with the same ShopID, if two courses have the same ShopID, it may return this course and another course in each call. Do not know what causes the bottom layer, put forward this method for reference only, if you know the reason can also leave a message ~