Closed venusdrogon closed 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); }
上述代码会有缺陷, 会改变元素的顺序,对于要求不是特别高的情况下可以使用
list 根据多个属性去重