Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does java.util.Set check for duplicates

Tags:

java

set

hashset

I have a very basic question, when does java.util.Set checks if the objects being added are duplicates?

Because I have a model class as below, which overrides both equals and hashcode methods

public class SampleModel implements Comparable {
    private String name;

    public SampleModel(String name) {
        this.name = name;
    }

    // Setter and Getter omitted
    @Override
    public boolean equals(Object arg0) {
        boolean eq = false;

        if (arg0 instanceof SampleModel
                && this.name.equalsIgnoreCase(((SampleModel) arg0).name)) {
            eq = true;
        }
        return eq;
    }

    @Override
    public int compareTo(Object arg0) {
        return this.name.compareTo(((SampleModel) arg0).name);
    }

    @Override
    public int hashCode() {
        return this.name.length();
    }
}

Then this is how I use the model objects in HashSet.

    SampleModel s1 = new SampleModel("Satya");
    SampleModel s2 = new SampleModel("Katti");

    Set<SampleModel> samSet = new HashSet<SampleModel>();
    System.out.println(samSet.add(s1));
    System.out.println(samSet.add(s2));

    s2.setName("Satya");
    System.out.println(s2.getName());
    System.out.println(s1 + ", " + s2);

As per equality clause the objects are same and equal but the HashSet will contain duplicates.

Is there any violation happening w.r.t equals or hashcode? If this code is perfectly alright then any ways to prevent from adding the duplicates?

I assume, whatever fields used in determining equals and hashcode should be made immutabe?

like image 855
Satya Avatar asked May 30 '12 10:05

Satya


1 Answers

I assume, whatever fields used in determining equals and hashcode should be made immutable?

That's right.

More precisely, the code in your question violates the following part of the Set contract:

Note: Great care must be exercised if mutable objects are used as set elements. The behavior of a set is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is an element in the set. A special case of this prohibition is that it is not permissible for a set to contain itself as an element.

Once you violate the contract, all bets are off.

like image 199
NPE Avatar answered Oct 16 '22 12:10

NPE