0

here is my attempt

public class blah {

HashSet<something> blah;    

public Blah() {
    this.blah = new HashSet<something>(); //this is empty constructor of something

}

// I want to copy all the element of Public blah in new Blah

public Blah(Blah initialBlah) {
    initialBlah = new Blah();
            // MAKE DEEP COPIES HERE 
            for (something c : blah){
                initialBlah.add(c);
            }

        }

I tried that but is not right HashSet newBlah = new HashSet();

Reboot
  • 63
  • 10
  • tried to read the [previous question](http://stackoverflow.com/questions/64036/how-do-you-make-a-deep-copy-of-an-object-in-java)? you will need that to copy the `HashSet` contents as.. so far, you're just copying the references.. – Bagus Tesa Apr 05 '17 at 01:55
  • How do I copy a content of initial hashset in a new hashset that is not same as first hashset in the memory – Reboot Apr 05 '17 at 01:59

1 Answers1

0

You are just copying the references. You need to copy the content fully to create a new Blah object. To achieve this, do this instead of your for loop

this.blah = new HashSet<something>(initblah.blah);
toro
  • 194
  • 5