Java's Set Interface and HashSet Library Class

How The Set Interface is Different from the List Interface

Unlike a List, a Set does not allow duplicate entries. In addition, the items in a Set are, by definition, unordered.

HashSet in Java



Dealing with Duplicates in a HashSet

Consider the following attempt to insert a duplicate item several times into a HashSet:

    public static void main(String [] args) {
        Set<String> original = new HashSet<String>();
        original.add("Item 1");
        original.add("Item 1");
        original.add("Item 1");        
        original.add("Item 1");
        
        System.out.println( original );
    }

Output

[Item 1]

Note that no compiler or run-time error results from this repeated attempt. Instead, the HashSet simply ignores any attempts to add "Item 1" once it is already present in the Set.

A Set is an Unordered Collection

Unlike a list, for which the objects are specifically numbered and, therefore, located at a specific position on the list, a Set does not specify the ordering of its elements. This can lead to some unusual behavior. Consider the example, below, which creates and prints a Set. Note how the output reorders the elements unexpectedly.

    public static void main(String [] args) {

        Set<String> original = new HashSet<String>();

        original.add("Item 1");
        original.add("Item 2");
        original.add("Item 3"); 
        original.add("Item 4");   
        original.add("Item 5");  

        System.out.println( original );
     }

Output


[Item 4, Item 3, Item 2, Item 1, Item 5]

Identifying Objects in a HashSet

What does it mean for an Object to be present in a HashSet? Consider the following program which creates a Dog, d1, and inserts it into a HashSet. The program then creates several other Dogs similar or identical to the first. Our goal is to try to identify which will show up as present in the HashSet:

    public static void main(String [] args) {

        HashSet<Dog> pack1 = new HashSet<Dog>();

        Dog d1 = new Dog("Rover", 5, 37.5); // name, age, weight

        Dog d2 = d1;

        Dog d3 = new Dog("Rover", 5, 37.5);

        Dog d4 = new Dog("Rover", 6, 37.5);

        

        pack1.add( d1 );

        System.out.println("d1 in pack ? : " + pack1.contains(d1));

        System.out.println("d2 in pack ? : " + pack1.contains(d2));

        System.out.println("d3 in pack ? : " + pack1.contains(d3));

        System.out.println("d4 in pack ? : " + pack1.contains(d4));        

    }

Output

d1 in pack ? : true

d2 in pack ? : true

d3 in pack ? : false

d4 in pack ? : false


Therefore, a shallow copy of an Object will always register as being present in a HashSet if the original was present. Other Dogs created at a separate memory location will not match a HashSet entry even if all their properties are identical to the first.