Results 1 to 4 of 4
  1. #1
    ZebV43 is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default How to make a copy of a set

    When working with destructive bulk operations on a set I know that I have to make a copy if I don't want to change the original set. I do not know how to make this copy though. I used this code in a recent exam but it was wrong can anyone explain why and what I should be doing?

    Java Code:
    Set<Integer> copyOfSet = new HashSet<Integer>(originalSet);

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You may have to explain what "wrong" means, and possible what "destructive bulk operations" are.

    The following works for me (that is, the set contents are altered and then restored:

    Java Code:
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    
    public class CopySet {
        public static void main(String[] args) {
            Set<Integer> originalSet = new HashSet<Integer>();
            originalSet.add(-1);
            originalSet.add(42);
            originalSet.add(666);
    		
            Set<Integer> copyOfSet = new HashSet<Integer>(originalSet);
    		
                // "destructive" operation on the original set
            Iterator<Integer> it = originalSet.iterator();
            while(it.hasNext()) {
                if(it.next() == 42) {
                    it.remove();
                }
            }
            //Set<Integer> toRemove = new HashSet<Integer>();
            //toRemove.add(42);
            //originalSet.removeAll(toRemove);
            System.out.println(originalSet);
    		
                // now restore
            originalSet = copyOfSet;
            System.out.println(originalSet);
        }
    }

  3. #3
    ZebV43 is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default

    Hi, thanks for replying so quickly.

    By wrong I was told that I was just creating another reference to the existing Set rather than creating a new object.

    The destructive bulk operations I meant were (set.addAll set.retainAll set.removeAll).

    In the actual exam I used a getter method to add the originalSet to the TreeSet constructor. Maybe this is what I did wrong?

    Java Code:
    Set<Integer> copyOfSet = new ArrayList<Integer>(this.getOriginalSet());
    But If the code in my original post works for you then I think I may have to assume that I did something else silly in the exam! As long as I have the right idea now I guess that is the important thing.

    Thanks for your help :-)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Both the new HashSet(Collection) and new ArrayList(Collection) give you a reference to a new collection containing the given elements (hence the "new") and certainly not a reference to an existing set or list.

    Using a "getter" method won't change this. (Assuming you "get" the set before you make changes to its contents.)

    One thing to remember is that the things referenced by the contents of the original set are not copied. So if you add and remove elements from the set you can get the original contents back again by using the copy. But if you change the state of the set elements then those changes that will affect the state of the copy elements (because the elements of the set and the copy reference the same objects.) This isn't a problem with Integer, but it may show up if you have a set of mutable things.

    The commented bit of the code I posted should prove to you that the copy is proof against changes made with removeAll(). And addAll() is similar.

Similar Threads

  1. Object copy
    By Oktam in forum New To Java
    Replies: 6
    Last Post: 07-21-2010, 09:09 AM
  2. Deep copy?!
    By deepthought015 in forum New To Java
    Replies: 3
    Last Post: 05-07-2009, 07:31 PM
  3. not compiling a copy of class -
    By itaipee in forum Eclipse
    Replies: 0
    Last Post: 01-07-2009, 10:47 AM
  4. Problems with copy paste
    By fredand44 in forum Eclipse
    Replies: 0
    Last Post: 12-17-2008, 05:14 PM
  5. USB Device Copy
    By Mir in forum New To Java
    Replies: 3
    Last Post: 08-25-2008, 12:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •