List集合去重的一些方法(常规遍历、Set去重、java8 stream去重、重写equals和hashCode方法)

public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("a");
        list.add("c");
        list.add("d");
        list.add("e");
        Deduplication1(list);
        System.out.println("---------");
        Deduplication2(list);
        System.out.println("---------");
        Deduplication3(list);
        System.out.println("---------");
        
    }
    
    // 遍历后判断赋给另一个list集合,保持原来顺序
    public static void Deduplication1(List<String> list) {
        System.out.println("list = " + list );
        List<String> newList = new ArrayList<String>();
        for(String str : list) {
            if(!newList.contains(str)) {
                newList.add(str);
            }
        }
        System.out.println("newList = " + newList);
    }
    
    // set集合去重,保持原来顺序
    public static void Deduplication2(List<String> list) {
        System.out.println("list = " + list );
        List<String> newList = new ArrayList<String>();
        Set set = new HashSet();
        for(String str : list) {
            if(set.add(str)) {
                newList.add(str);
            }
        }
        System.out.println("newList = " + newList);
    }
    
    // Set去重     由于Set的无序性,不会保持原来顺序
    public static void Deduplication3(List<String> list) {
        System.out.println("list = " + list );
        Set set = new HashSet();
        List<String> newList = new ArrayList<String>();
        set.addAll(list);
        newList.addAll(set);
        System.out.println("newList = " + newList);
    }

console结果:

list = [a, b, a, c, d, e]
newList = [a, b, c, d, e]
---------
list = [a, b, a, c, d, e]
newList = [a, b, c, d, e]
---------
list = [a, b, a, c, d, e]
newList = [d, e, b, c, a]
---------

上面的方法在List元素为基本数据类型及String类型时是可以的,但是如果List集合元素为对象,却不会奏效

class User {
    private String name;
    private int age;
    
    public User() {
    }
    
    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    
    @Override
    public String toString() {
        return "User [name=" + name + ", age=" + age + "]";
    }
    
}
public static void main(String[] args) {
        List<User> userList = new ArrayList<User>();
        userList.add(new User("小黄",10));
        userList.add(new User("小红",23));
        userList.add(new User("小黄",78));
        userList.add(new User("小黄",10));
        
        //使用HashSet,无序
        Set<User> userSet = new HashSet<User>();
        userSet.addAll(userList);
        System.out.println(userSet);
        
        //使用LinkedHashSet,有序
        List<User> listNew = new ArrayList<User>(new LinkedHashSet(userList));
        System.out.println(listNew.toString());
        
    }

console结果:

[User [name=小红, age=23], User [name=小黄, age=10], User [name=小黄, age=78], User [name=小黄, age=10]]
[User [name=小黄, age=10], User [name=小红, age=23], User [name=小黄, age=78], User [name=小黄, age=10]]

解决方法:对象中重写equals()方法和hashCode()方法

class User {
    private String name;
    private int age;
    
    public User() {
    }
    
    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    
    @Override
    public String toString() {
        return "User [name=" + name + ", age=" + age + "]";
    }
    
    @Override
    public boolean equals(Object obj) {
        User user = (User)obj;
        return this.name.equals(user.getName()) && (this.age == user.getAge());
    }
    
    @Override
    public int hashCode() {
        String str = this.name + this.age;
        return str.hashCode();
    }
}

console结果:

[User [name=小红, age=23], User [name=小黄, age=78], User [name=小黄, age=10]]
[User [name=小黄, age=10], User [name=小红, age=23], User [name=小黄, age=78]]

采用java8的stream来实现去重

对于不是对象的list去重:

//利用java8的stream去重
  List uniqueList = list.stream().distinct().collect(Collectors.toList());
  System.out.println(uniqueList.toString());

如果List集合元素为对象:

//根据name属性去重
        List<User> unique1 = userList.stream().collect(
                collectingAndThen(
                        toCollection(() -> new TreeSet<>(comparing(User::getName))), ArrayList::new));

        System.out.println(unique1.toString());

        //根据name,age属性去重
        List<User> unique2 = userList.stream().collect(
                collectingAndThen(
                        toCollection(() -> new TreeSet<>(comparing(o -> o.getName() + ";" + o.getAge()))), ArrayList::new)
        );

        System.out.println(unique2.toString());

 

转自:https://www.cnblogs.com/zjfjava/p/9897650.html

posted @ 2019-05-05 18:04  myseries  阅读(1208)  评论(0编辑  收藏  举报