1.根据对象的某个属性去重:
网上找的stream流去重方法,可以根据类的某个属性去重,这里记录一下
public static Predicate distinctNotByKey(Function super T, ?> keyExtractor) { Map
使用:
public static void testC() { List list = new ArrayList<>(); UserAccount a = new UserAccount(); a.setId(57L); UserAccount b = new UserAccount(); b.setId(57L); UserAccount c = new UserAccount(); c.setId(56L); list.add(a); list.add(b); list.add(c); //根据id去重 List collect = list.stream().filter(distinctByKey(UserAccount::getId)).collect(Collectors.toList()); System.out.println(collect); System.out.println(collect.size()); }
来源地址:https://blog.csdn.net/weixin_38860401/article/details/131447130