Copy Java Sets

Is there a way to copy a TreeSet ? That is, is it possible to go

 Set <Item> itemList; Set <Item> tempList; tempList = itemList; 

or do you need to physically iterate over the sets and copy them one by one?

+77
java set copy
Sep 24 2018-11-11T00:
source share
4 answers

Another way to do this is to use the copy constructor :

 Collection<E> oldSet = ... TreeSet<E> newSet = new TreeSet<E>(oldSet); 

Or create an empty set and add the elements:

 Collection<E> oldSet = ... TreeSet<E> newSet = new TreeSet<E>(); newSet.addAll(oldSet); 

Unlike clone , they allow you to use a different collection class, a different comparator, or even populate a different (undefined) collection type.




Note that the result of copying a [TG43] is a new [TG44] containing references to the objects that are elements if the original [TG45]. The element objects themselves are not copied or cloned. This conforms with the way that the Java [TG46] APIs are designed to work: they don't copy the element objects.

+146
Sep 24 '11 at 6:14
source share
β€” -

In Java 8, you can use stream and collect to copy elements:

 Set<Item> newSet = oldSet.stream().collect(Collectors.toSet()); 

Or you can compile in an ImmutableSet (if you know that the set should not change):

 Set<Item> newSet = oldSet.stream().collect(ImmutableSet.toImmutableSet()); 
+6
Oct 11 '17 at 15:40
source share

The copy constructor given by @Stephen C is the way you created Set (or when you know where it came from). When this comes from Map.entrySet() , it will depend on the Map implementation you use:

findbugs says

The entrySet () method allows you to return a Map view in which one Entry is reused and returned during iteration. Starting with Java 1.6, both IdentityHashMap and EnumMap have done this. When repeating using such a Map, the Entry value is valid only until you proceed to the next iteration. If, for example, you try to pass such an entrySet method for the addAll method, everything will be bad.

Since addAll() is called by the copy constructor, you can find yourself with a set of only one record: the last.

Not all Map implementations do this, so if you know that your implementation is safe in this regard, the copy constructor is definitely the way to go. Otherwise, you will have to create new Entry objects yourself:

 Set<K,V> copy = new HashSet<K,V>(map.size()); for (Entry<K,V> e : map.entrySet()) copy.add(new java.util.AbstractMap.SimpleEntry<K,V>(e)); 

Edit: Unlike the tests I ran on Java 7 and Java 6u45 (thanks to Stephen C), the findbugs comment no longer fits. This may have been the case in earlier versions of Java 6 (before u45), but I don't need to test.

+3
Sep 18 '15 at 15:49
source share

Starting with Java 10 :

 Set<E> oldSet = Set.of(); Set<E> newSet = Set.copyOf(oldSet); 

Set.copyOf() returns an immutable Set containing the elements of this Collection .

This Collection must not be null and must not contain any null elements.

+3
Jun 26 '18 at 15:42
source share



All Articles