集合根据一个或多个字段去重

实体类

package com.wgjn.modules.cms.pojo;

import java.util.Date;

public class Person {
    private String userName;
    private Integer age;
   /* private Date createTime;*/

    public Person() {

    }

    public Person(String name, int age) {
        this.userName = name;
        this.age = age;

    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }



    public Person(String userName, Integer age, Date createTime) {
        this.userName = userName;
        this.age = age;

    }
}

根据单个字段去重hashset方法

  public static void main(String[] args) {
        List<Person> list = new ArrayList<>();

        list.add(new Person("张三",12));
        list.add(new Person("张三",13));
        list.add(new Person("李四", 14));
        List<Person> list1= new ArrayList<Person>();
        Set<String> set=new HashSet<String>();
        for (Person person : list) {
            if (person == null) {
                continue;
            }
            String  userName = person.getUserName();
            if (userName != null) {
                if (!set.contains(userName)) { //set中不包含重复的
                    set.add(userName);
                    list1.add(person);
                    System.out.println(list1.size());
                } else {
                    continue;
                }
            }
        }


        set.clear();
        for (int i = 0; i <list1.size() ; i++) {

        System.out.println(list1.get(i).getUserName());
        }
    }

单个字段去重hashmap方法

 public static void main(String[] args) {
        List<Person> list = new ArrayList<>();

        list.add(new Person("张三",12));
        list.add(new Person("张三",13));
        list.add(new Person("李四", 14));

        List<Person> list1= new ArrayList<Person>();
        HashMap<String, String> hashMap = new HashMap<String, String>();
        for (Person person : list) {
            if (person == null) {
                continue;
            }
            String  userName = person.getUserName();
            if (userName != null) {
                // String listIdStr = listId.longValue() + "";
                String value = hashMap.get(userName);
                if (StringUtils.isBlank(value)) { //如果value是空的  说明取到的这个userName是第一次取到
                    hashMap.put(userName, userName);
                    list1.add(person); //list1就是我们想要的去重之后的结果
                } else {
                    continue;
                }
            }
        }

        hashMap.clear();//结束了记得将hashMap请一个,内存能够得到及时的释放



        for (int i = 0; i <list1.size() ; i++) {

        System.out.println(list1.get(i).getUserName());
        }
    }


多个字段去重hashmap方法

public static void main(String[] args) {
        List<Person> list = new ArrayList<>();

        list.add(new Person("张三", 12));
        list.add(new Person("张三", 13));
        list.add(new Person("李四", 14));

        List<Person> list1 = new ArrayList<Person>();
        HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
        for (Person person : list) {
            if (person == null) {
                continue;
            }
            String userName = person.getUserName();
            int userAge = person.getAge();


            if (userName != null) {

                if (hashMap.get(userName) == null) {
                    hashMap.put(userName, userAge);
                    list1.add(person); //list1就是我们想要的去重之后的结果
                } else if (userAge == hashMap.get(userName)) {
                    continue;
                } else {
                    hashMap.put(userName, userAge);
                    list1.add(person); //list1就是我们想要的去重之后的结果

                }

            }
        }

        hashMap.clear();//结束了记得将hashMap请一个,内存能够得到及时的释放


        for (int i = 0; i < list1.size(); i++) {

            System.out.println(list1.get(i).getUserName());
        }
    }

单个字段去重TreeSet方法

package com.wgjn.modules.cms.controller;

import com.wgjn.modules.cms.pojo.Person;
import org.apache.commons.lang.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

public class ceshi {
    public static void main(String[] args) {
        List<Person> list = new ArrayList<>();

        list.add(new Person("张三", 12));
        list.add(new Person("张三", 13));
        list.add(new Person("李四", 14));

        List<Person> list1=removeDuplicate(list);
        for (int i = 0; i < list1.size(); i++) {

            System.out.println(list1.get(i).getUserName());
        }
    }
    public static List removeDuplicate(List list) {
        // 按照订单编号去重
        list = (List) list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()
                -> new TreeSet<>(Comparator.comparing(Person::getUserName))), ArrayList::new));

        return list;
    }


}

多个属性去重Treeset方法

package com.wgjn.modules.cms.controller;

import com.wgjn.modules.cms.pojo.Person;
import org.apache.commons.lang.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

public class ceshi {
    public static void main(String[] args) {
        List<Person> list = new ArrayList<>();

        list.add(new Person("张三", 12));
        list.add(new Person("张三", 13));
        list.add(new Person("李四", 14));

      /*  ArrayList<Person> list1 = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()
                -> new TreeSet<>(Comparator.comparing(o -> o.getAge() + ";" + o.getUserName()))), ArrayList::new));
      */
      List<Person> list1=removeDuplicate(list);
        for (int i = 0; i < list1.size(); i++) {

            System.out.println(list1.get(i).getUserName());
        }
    }
    public static List<Person> removeDuplicate(List<Person> list) {
        // 按照订单编号去重
        ArrayList<Person> list1 = list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(()
                -> new TreeSet<>(Comparator.comparing(o -> o.getAge() + ";" + o.getUserName()))), ArrayList::new));
        return list1;
    }


}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值