user1236048
user1236048

Reputation: 5602

java.lang.UnsupportedOperationException when combining two Sets

I have 2 different instances of HashMap

I want to merge the keysets of both HashMaps;

Code:

Set<String> mySet = hashMap1.keySet();
mySet.addAll(hashMap2.keySet());

Exception:

java.lang.UnsupportedOperationException
    at java.util.AbstractCollection.add(AbstractCollection.java:238)
    at java.util.AbstractCollection.addAll(AbstractCollection.java:322)

I don't get a compile warning or error.

From java doc this should work. Even if the added collection is also a set:

boolean addAll(Collection c)

Adds all of the elements in the specified collection to this set if they're not already present (optional operation). If the specified collection is also a set, the addAll operation effectively modifies this set so that its value is the union of the two sets. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress.

Upvotes: 54

Views: 47179

Answers (5)

Roberto Rodriguez
Roberto Rodriguez

Reputation: 3347

Just create your own Set with the keys of the Map like this:

Set set = new HashSet(map.keySet()); 

Then you can add whatever you want to it.

Upvotes: 0

nantitv
nantitv

Reputation: 3733

All the above answers are correct. If you still wants to know the exact implementation detail (jdk 8)

hashMap1.keySet() returns a KeySet<E> and

KeySet<E>   extends AbstractSet<E>
AbstractSet<E> extends AbstractCollection<E> 

In AbstractCollection,

public boolean add(E e) {
        throw new UnsupportedOperationException();
    }

addAll() calls add() and thats why you are getting an UOException

Upvotes: 2

Rahul
Rahul

Reputation: 45060

If you look at the docs of the HashMap#keySet() method, you'll get your answer(emphasis mine).

Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the Iterator.remove, Set.remove, removeAll, retainAll, and clear operations. It does not support the add or addAll operations.

Therefore, you need to create a new set and add all the elements to it, instead of adding the elements to the Set returned by the keySet().

Upvotes: 61

Vinay Veluri
Vinay Veluri

Reputation: 6855

Doesn't support by nature of Set which is from map.keySet(). It supports only remove, removeAll, retainAll, and clear operations.

Please read documentation

Upvotes: 1

NPE
NPE

Reputation: 500297

The result of keySet() does not support adding elements to it.

If you are not trying to modify hashMap1 but just want a set containing the union of the two maps' keys, try:

Set<String> mySet = new HashSet<String>();
mySet.addAll(hashMap1.keySet());
mySet.addAll(hashMap2.keySet());

Upvotes: 34

Related Questions