Creating a Copy of a Collection

These examples create a shallow copy of a collection. That is, the new collection contains references to same objects as the source collection; the objects are not cloned.
List stuff = Arrays.asList(new String[]{"a", "b"}); // Make a copy of a list List list = new ArrayList(stuff); List list2 = new LinkedList(list); // Make a copy of a set Set set = new HashSet(stuff); Set set2 = new TreeSet(set); // Make a copy of a map Map map = new HashMap(); // Add key/value pairs ... Map map2 = new TreeMap(map);

Comments

14 Jul 2010 - 3:48am by Bipin Patel (not verified)

I like ur site and thanks for givinf me this type of example
really i understand ur language that very easy to understand every user

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.