java中集合去重2

时间:2022-09-03 16:30:49

1、对集合中的自动定义的对象去重:

  自定义Person类,同时复写hashCode和equals方法

package collection;

public class Person {
    
    private String username;
    
    private String password;
    

    public Person() {
        super();
    }

    public Person(String username, String password) {
        super();
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "Person [username=" + username + ", password=" + password + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((password == null) ? 0 : password.hashCode());
        result = prime * result
                + ((username == null) ? 0 : username.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Person other = (Person) obj;
        if (password == null) {
            if (other.password != null)
                return false;
        } else if (!password.equals(other.password))
            return false;
        if (username == null) {
            if (other.username != null)
                return false;
        } else if (!username.equals(other.username))
            return false;
        return true;
    }

}

测试去重代码:

@Test
    public void test3(){
        
        Person p1 = new Person("张三","zhangsan");
        
        Person p2 = new Person("李四","lisi");
        
        Person p3 = p1;
        
        Person p4 = p2;
        
        Person p5 = new Person("张三","zhangsan");
        
        Person p6 = new Person("李四","lisi");
        
        
        List<Person> list1 = new ArrayList<Person>();
        list1.add(p1);
        list1.add(p2);
        list1.add(new Person("小周","xiaozhou"));
        list1.add(p3);
        list1.add(p4);
        list1.add(new Person("小李","小李"));
        list1.add(p5);
        list1.add(p6);
        logger.info(list1);
        //Set<Person> set = new HashSet<Person>(list1);
        
        
        
        List<Person> list2 = new ArrayList<Person>();
        if(list2.isEmpty()){
            System.out.println("list2是空");
        }
        
        for (Person person : list1) {
            if (!(list2.contains(person))) {
                list2.add(person);
            }
        }
//        list1.clear();
//        list1.addAll(list2);
        logger.info(list2);
    }
    
    @Test
    public void test4(){
        
        Set<Person> set = new LinkedHashSet<Person>();
        set.add(new Person("张三", "zhangsan"));
        set.add(new Person("张三", "zhangsan"));
        set.add(new Person("张三", "12"));
        set.add(new Person("李四", "21"));
        set.add(new Person("李四", "21"));
        set.add(new Person("李四", "45"));
        set.add(new Person("王五", "23"));
        
        /*for (Iterator<Person> it = set.iterator(); it.hasNext();) {
            Person person = it.next();
            
            logger.info(person);
        }*/
        
        logger.info(set);
    }

补充:

List和Set集合的区别在于:

list集合可以存在相同的元素,set集合则不能。但是同一个元素是根据对象在内存中地址的哈希吗来比较的,也就是说new出的每一个对象在内存中的地址都是不同的。所以set集合想要去重username和password相同的person对象,不一定是可以做到的,除非每次往集合中添加的对象,都是指向之前已经存在容器中对象地址的引用。所以这里我们要复写person类中的hashCode和equals方法。

同时我们又知道set集合中的hashset的数据结构是hash表(散列的数据结构),为了让去重后的set集合依然是有序的,所以这里可以使用hashset的子类linkedhashset,这样去重后的set集合是,有顺序的。