list根据对象的属性值去掉重复元素

1.对于list 类型的去重。

1. 正序删除,取对象的属性中第一个重复的 对象组成list, 删除后续list中与第一个对象的id相等的元素, 

    public void removeDuplicate(List personVoList) {
        if(CollectionUtils.isEmpty(personVoList)){
            return ;
        }
        
        for (int i = 0; i < personVoList.size(); i++) {
            PersonVo personVo = personVoList.get(i);
            String a00 = personVo.getA00();
            for (int j = i+1; j < personVoList.size(); j++) {
                PersonVo vo = personVoList.get(j);
                if(StringUtils.endsWithIgnoreCase(a00, vo.getA00())){
                    personVoList.remove(j);
                    j--;
                }
            }
        }
    }

输出结果:

正序删除后:

去重前:[PersonVo(a00=111, name=aaa, charge=0), PersonVo(a00=222, name=bbb, charge=0), PersonVo(a00=333, name=ccc, charge=0), PersonVo(a00=111, name=ddd, charge=0), PersonVo(a00=111, name=eee, charge=1), PersonVo(a00=444, name=fff, charge=0)]
去重后:[PersonVo(a00=111, name=aaa, charge=0), PersonVo(a00=222, name=bbb, charge=0), PersonVo(a00=333, name=ccc, charge=0), PersonVo(a00=444, name=fff, charge=0)]
排序后aaa,bbb,ccc,fff

测试数据:

 public static void main(String[] args) {
        List list = new ArrayList<>();
        PersonVo user = new PersonVo();
        user.setA00("111");
        user.setName("aaa");
        user.setCharge("0");

        PersonVo user1 = new PersonVo();
        user1.setA00("222");
        user1.setName("bbb");
        user1.setCharge("0");

        PersonVo user2 = new PersonVo();
        user2.setA00("333");
        user2.setName("ccc");
        user2.setCharge("0");

        PersonVo user3 = new PersonVo();
        user3.setA00("111");
        user3.setName("ddd");
        user3.setCharge("0");

        PersonVo user4 = new PersonVo();
        user4.setA00("111");
        user4.setName("eee");
        user4.setCharge("1");
        PersonVo user5 = new PersonVo();
        user5.setA00("444");
        user5.setName("fff");
        user5.setCharge("0");

        list.add(user);
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        list.add(user5);
        DeptBaseInfService ll = new DeptBaseInfService();
        System.out.println("去重前:"+list);
        //倒序删除 时使用,Collections.reverse(list);
        ll.removeDuplicate(list);
        System.out.println("去重后:"+list);

    }

2.逆序删除,保留重复id的最后一个元素。

a. 使用双重for循环删除,可以先倒序list. 再执行

removeDuplicate 方法。 
Collections.reverse(personVoList);

        for (int i = 0; i < list.size(); i++) {
            PersonVo personVo = list.get(i);
            String a00 = personVo.getA00();
            for (int j = i+1; j < list.size(); j++) {
                PersonVo vo = list.get(j);
                if(StringUtils.endsWithIgnoreCase(a00, vo.getA00())){
                    list.remove(j);
                    j--;
                }
            }
        }

b. 使用stream流快捷删除(推荐使用)

Collections.reverse(personVoList);

ArrayList collect = personVoList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(
                    Comparator.comparing(PersonVo::getA00)
            )), ArrayList::new));
// 生成了一个新的list

逆序删除后,测试结果:

去重前:[PersonVo(a00=111, name=aaa, charge=0), PersonVo(a00=222, name=bbb, charge=0), PersonVo(a00=333, name=ccc, charge=0), PersonVo(a00=111, name=ddd, charge=0), PersonVo(a00=111, name=eee, charge=1), PersonVo(a00=444, name=fff, charge=0)]
去重后:[PersonVo(a00=444, name=fff, charge=0), PersonVo(a00=111, name=eee, charge=1), PersonVo(a00=333, name=ccc, charge=0), PersonVo(a00=222, name=bbb, charge=0)]
排序后eee,fff,ccc,bbb

 2. list的简单删除,初级业务使用List 类型

     1. for循环删除

public static List removeDuplicate1(List list) {
    for (int i = 0; i < list.size() - 1; i++) {
        for (int j = list.size() - 1; j > i; j--) {
            if (list.get(j).equals(list.get(i))) {
                list.remove(j);
            }
        }
    }
    return list;
}

     2. 使用迭代器删除

public static void removeDuplicateWithOrder3(List list) {
    Set set = new HashSet();
    List newList = new ArrayList();
    for (Iterator iter = list.iterator(); iter.hasNext(); ) {
        Object element = iter.next();
        if (set.add(element))
            newList.add(element);
    }
    list.clear();
    list.addAll(newList);
    System.out.println(" remove duplicate " + list);
}

3. 使用hashset 删除,再将hashset 转化为list

ArrayList numbersList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8)); 
        System.out.println(numbersList);
        LinkedHashSet hashSet = new LinkedHashSet<>(numbersList);

 List list = new ArrayList(hashSet);

    4. stream流删 除

 list.stream().distinct().collect(Collectors.toList());

 3. stream流多个条件去重(待测试

多个字段或者多个条件去重

ArrayList collect1 = patentDtoList.stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(
                        Comparator.comparing(p->p.getPatentName() + ";" + p.getLevel()))), ArrayList::new)

你可能感兴趣的:(springboot,java,异常报错,算法,c++,数据结构)