Adding custom objects in the collection Set

Issues discussed: As we all know, is the set of characteristics set no duplicate elements. General Java class library types such as String class after adding to the set, and will not duplicate the phenomenon. Then the custom class it?

1. Create a custom class

public class Dog {
    private String name;
    private String kind;
    private String hobby;
    
    public Dog(String name,String kind,String hobby) {
        this.hobby=hobby;
        this.kind=kind;
        this.name=name;
    }

    public String getName() {
        return name;
    }

    public String getKind() {
        return kind;
    }

    public String getHobby() {
        return hobby;
    }

    @Override
    public String toString() {
        return "Dog [name=" + name + ", kind=" + kind + ", hobby=" + hobby + "]";
    }

2. Test

public class TestSet {

    public static void main(String[] args) {
        
        Set<Dog> dogs=new HashSet<>();
        
        Dog dog=new Dog("huahua","tianyuan","run");
        Dog dog1=new Dog("tiantian","hashiqi","eat");
        Dog dog2=new Dog("qiqi","jinmao","smell");
        Dog dog3=new Dog("qiqi","jinmao","smell");
        dogs.add(dog);
        dogs.add(dog1);
        dogs.add(dog2);
        dogs.add(dog3);
        System.out.println(dogs);
    }
}

3. Results:

[Dog [name=qiqi, kind=jinmao, hobby=smell], Dog [name=huahua, kind=tianyuan, hobby=run], Dog [name=tiantian, kind=hashiqi, hobby=eat], Dog [name=qiqi, kind=jinmao, hobby=smell]]

4. Analysis:

It can be seen to have two elements in the structure.

When a custom class needs to be rewritten hashcode () and equals () method, whether the equals Object () is determined only two objects have the same references, the contents of details of the object can not be determined, it is necessary to rewrite.

Note: hashcode is according to a random integer class instance field generated, it may be negative. As long as the same two objects, their code on the same hashcode.

5. improvement: in a method of rewriting Dog class:

@Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((hobby == null) ? 0 : hobby.hashCode());
        result = prime * result + ((kind == null) ? 0 : kind.hashCode());
        result = prime * result + ((name == null) ? 0 : name.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;
        Dog other = (Dog) obj;
        if (hobby == null) {
            if (other.hobby != null)
                return false;
        } else if (!hobby.equals(other.hobby))
            return false;
        if (kind == null) {
            if (other.kind != null)
                return false;
        } else if (!kind.equals(other.kind))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

6. Results:

[Dog [name=tiantian, kind=hashiqi, hobby=eat], Dog [name=qiqi, kind=jinmao, hobby=smell], Dog [name=huahua, kind=tianyuan, hobby=run]]

There are no repeated elements!

 

Guess you like

Origin www.cnblogs.com/liu-chen/p/11877759.html