Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unmodifiable collection issue when retainall is used for two key sets in java

Map<String,String> map=request.getParameterMap();

^ is the unmodifiable map.

Set s1= map.keySet();
Set s2= map2.keySet();/* another keyset of local map*/

Using s1.retainAll(s2) throws an exception: at java.util.collections$unmodifiablecollection.retainall

Here request.getParameterMap() returns an unmodifiable map.. I tried creating a local map. But the issue stil persists. Suggest some solution.

like image 207
knix2 Avatar asked Nov 21 '25 11:11

knix2


1 Answers

The Set.retainAll method modifies the set it's being called on. Assuming the keySet method of you unmodifiable map is just a view on to the underlying map, it shouldn't allow modifications. You probably want to create a new (modifiable) set and then remove items from it:

Set s1 = new HashSet(map.keySet());
s1.retainAll(s3);
like image 81
SimonC Avatar answered Nov 24 '25 01:11

SimonC