ifeilong / feilong-core

:gem: Reduce development, Release ideas
Apache License 2.0
529 stars 155 forks source link

list 根据多个属性去重 #808

Closed venusdrogon closed 4 years ago

venusdrogon commented 4 years ago

list 根据多个属性去重


    /**
     * 根据list中对象某些字段去重
     * @param list 需要去重的list
     * @return 返回去重后的list
     */
    private static List<PromotionSKUDiscAMTBySetting> removeDuplicateOutputField(List<PromotionSKUDiscAMTBySetting> list) {
        Set<PromotionSKUDiscAMTBySetting> set = new TreeSet<>(new Comparator<PromotionSKUDiscAMTBySetting>() {
            @Override
            public int compare(PromotionSKUDiscAMTBySetting o1, PromotionSKUDiscAMTBySetting o2) {
                if(Objects.equals(o1.getPromotionId(), o2.getPromotionId()) && Objects.equals(o1.getSkuId(), o2.getSkuId())) {
                    return 0;//==0表示重复
                }
                return 1;
            }
        });
        set.addAll(list);
        return new ArrayList<>(set);
    }   
venusdrogon commented 4 years ago

上述代码会有缺陷, 会改变元素的顺序,对于要求不是特别高的情况下可以使用