Results 1 to 4 of 4
  1. #1
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Collections copy constructors guaranteed shallow?

    The doc for the clone() method of HashSet explicitly says it returns a shallow copy. I have tested that the copy constructor also produces shallow copies. But is this behavior guaranteed?

    Java Code:
    import java.util.HashSet;
    import java.util.Set;
    
    public class Shallow {
    
    	public static void main(String[] args) {
    		Shallow f = new Shallow();
    		Set<Shallow> s1 = new HashSet<Shallow>();
    		s1.add(f);
    		Set<Shallow> s2 = new HashSet<Shallow>(s1);
    		System.out.println("Copy constructor is shallow: " + s2.contains(f));
    	}
    	
    }
    Get in the habit of using standard Java naming conventions!

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

    Default Re: Collections copy constructors guaranteed shallow?

    The clone() method of HashSet produces a new HashSet with exactly the same contents as the set being cloned. This is more or less the same behaviour as the HashSet constructor which takes a reference to a Collection instance as an argument which will produce a set with exactly the same elements as the collection. (but not necessarily the same cardinality...)

    Since collections (sets or otherwise) contain only references neither clone() nor the constructor would live up to their documented contracts if they cloned the contents, since cloning would produce new (ie different) contents. In as much as the constructors create collections with the same contents they can't clone those contents. Of course Collection is an interface so that behaviour can't be guaranteed...

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Collections copy constructors guaranteed shallow?

    Makes sense. Thanks.
    Get in the habit of using standard Java naming conventions!

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

    Default Re: Collections copy constructors guaranteed shallow?

    You're welcome.

Similar Threads

  1. On references and copy constructors
    By guest_user in forum New To Java
    Replies: 4
    Last Post: 05-25-2011, 01:27 PM
  2. Replies: 1
    Last Post: 05-25-2011, 10:01 AM
  3. Collections
    By NatureFreshMilk in forum New To Java
    Replies: 12
    Last Post: 05-23-2011, 09:12 AM
  4. Collections
    By Cbani in forum New To Java
    Replies: 3
    Last Post: 02-16-2010, 03:46 PM
  5. Shallow Copy Test
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 12:04 AM

Posting Permissions

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