list<bean> 使用java8根据某几个属性去重复

时间:2020-8-31 作者:admin


1:需求

	根据bean对象的某几个属性去重

2:distinct()的不足

distinct是根据bean的hash与equals方法去重,达不到本次需求的要求

3:数据准备

@Data
public class Dish {

    private String name;    //菜的名称

    private Boolean vegetaian;  //是否为素

    private Integer calories;   //卡路里

    private Type type;      //类型(肉 鱼 其他)

    public Dish() {

    }

    public Dish(String name, Boolean vegetaian, Integer calories, Type type) {
        this.name = name;
        this.vegetaian = vegetaian;
        this.calories = calories;
        this.type = type;
    }

    public enum Type {MEAT, FISH, OTHER} //肉 鱼 其他
}

public class DishList {

    //数据准备
    public static List<Dish> getDish1List() {
        return Arrays.asList(
                new Dish("pork", false, 800, Dish.Type.MEAT),
                new Dish("pork", false, 800, Dish.Type.MEAT),
                new Dish("beef", false, 700, Dish.Type.MEAT),
                new Dish("beef", false, 701, Dish.Type.MEAT),
                new Dish("chicken", false, 400, Dish.Type.MEAT),
                new Dish("french fries", true, 530, Dish.Type.OTHER),
                new Dish("rice", true, 350, Dish.Type.OTHER),
                new Dish("season fruit", true, 120, Dish.Type.OTHER),
                new Dish("pizza", true, 550, Dish.Type.OTHER),
                new Dish("prawns", false, 300, Dish.Type.FISH),
                new Dish("salmon", false, 450, Dish.Type.FISH)
        );
      }
}

4:去重要求

根据type-name去重的

    /**
     * 根据type-name去重的Function
     *
     * @return
     */
    public static Function<Dish, String> distinctByKeyFunction() {
        return (Dish dish) -> dish.getType() + "-" + dish.getName();
    }

5:解决方案

方案1:使用TreeSet

    @Test
    public void distinctByKey1() {
        List<Dish> dish1List = DishList.getDish1List();
        List<Dish> distinctDishList = dish1List.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(distinctByKeyFunction()))), ArrayList::new));
        System.out.println(distinctDishList);
    }

先将list转成TreeSet(TreeSet含有比较器)过滤重复数据,再将转换为 ArrayList

collectingAndThen的使用链接

方案二:封装通用方法

方法:

public static <T> Predicate<T> distinctByKey(Function<? super T, ?> function) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(function.apply(t), Boolean.TRUE) == null;
    }

测试:

    @Test
    public void distinctByKey2() {
        List<Dish> dish1List = DishList.getDish1List();
        List<Dish> distinctDishList = dish1List.stream().filter(distinctByKey(distinctByKeyFunction())).collect(Collectors.toList());
        System.out.println(distinctDishList);

    }
方法2辅助理解
        public static Predicate<Dish> distinctByKeyOriginal() {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        System.out.println(seen.size());
        Predicate<Dish> predicate = new Predicate<Dish>() {
            @Override
            public boolean test(Dish dish) {
                String key = dish.getType() + "" + dish.getName();
                Boolean containsKey = seen.containsKey(key);
                if (Boolean.TRUE.equals(containsKey)) {
                    System.out.println(false);
                    return false;
                } else {
                    seen.put(key, Boolean.TRUE);
                    System.out.println(true);
                    return true;
                }
            }
        };
        return predicate;
    }

String key = dish.getType() + “” + dish.getName(); 将其摘出,使用Function自由组合,就成了 “function.apply(t)”。

t -> seen.putIfAbsent(function.apply(t), Boolean.TRUE) == null;使用匿名内部类形式替换

两个 list<bean> 使用java8根据某几个属性去重复

方案二学习链接

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。