Cloning Objects in a HashSet

Cloning a HashSet Makes a Deep Copy of the Set But Shallow Copies of the Objects in the Set


Using the clone method can be used to create a deep copy of certain library classses such as a Set, but the elements in the original and duplicate Sets will remain shallow copies. Note that in the Oracle documentation for the clone method, it states that the copies are shallow. It is referring to the Objects in the Sets, not the Sets, themselves, that are shallow.

In the example, below, we create a Set of Dogs called pack1 and then clone the Set to a duplicate Set called pack2. When we remove an element from pack1 (fifi), it remains in pack2 because the Sets are deep copies, not shallow. However, when we change snoopy's name to red baron, it changes in both sets because the Dog objects are shallow copies of each other. Also note that the order in which the dogs are printed in the output for the two sets is not the same. Order is not guaranteed in a Set.

import java.util.*;
public class Dog
{
    private String name;
    public Dog(String name) { this.name = name;  }
    
    public void setName(String name) { this.name = name; }

    @Override public String toString() { return name; }
    
    public static void main(String [] args) {
        
        HashSet<Dog> pack1 = new HashSet<Dog>();
        Dog rover = new Dog("Rover");
        Dog fifi = new Dog("Fifi");
        Dog snoopy = new Dog("Snoopy");
        
        pack1.add( rover );
        pack1.add( fifi );
        pack1.add( snoopy );
        HashSet<Dog> pack2 = new HashSet<Dog>();
        pack2 = (HashSet)pack1.clone();
        pack1.remove(fifi);
        snoopy.setName( "red baron" );
        System.out.println( pack1 );
        System.out.println( pack2 );
        
    }
}

Output:


[Rover, red baron]
[red baron, Rover, Fifi]

Another Example: Cloning a HashSet

    public static void main(String [] args) {
        HashSet<String> original = new HashSet<String>();
        original.add("Item 1");
        original.add("Item 2");
        original.add("Item 3");        
        original.add("Item 4");
        HashSet duplicate = new HashSet<String>();
        duplicate = (HashSet)original.clone();
        original.remove("Item 2");
        System.out.println( original );
        System.out.println( duplicate );
     
    }

Output:


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

Cloning a HashSet When the Pointers Have Been Deprecated


    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");
        Set duplicate = new HashSet<String>();
        duplicate = (Set)((HashSet)original).clone();
        original.remove("Item 2");
        System.out.println( original );
        System.out.println( duplicate );
     
    }

Output:


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