Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

list 根据多个属性去重 #808

Closed
venusdrogon opened this issue Dec 19, 2019 · 1 comment
Closed

list 根据多个属性去重 #808

venusdrogon opened this issue Dec 19, 2019 · 1 comment
Assignees
Milestone

Comments

@venusdrogon
Copy link
Collaborator

venusdrogon commented Dec 19, 2019

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 venusdrogon self-assigned this Dec 19, 2019
@venusdrogon venusdrogon added this to the 2.0.3 milestone Jan 6, 2020
@venusdrogon
Copy link
Collaborator Author

venusdrogon commented Jan 6, 2020

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant