Try
Set<String> both = new HashSet<String>(v1.keySet());
instead of
Set<String> both = v1.keySet();
You shouldn't modify set you got from keySet
method because map uses it and when you remove elements from it (by retainAll
in your case) elements also removed from map. Example:
Map<Integer, Integer> mp = new HashMap<Integer, Integer>();
mp.put(1, 1);
System.out.println(mp); // output {1=1}
mp.keySet().remove(1);
System.out.println(mp); // output {}