Q:

Remove values from Map while comparing the values against a List

Beginner QQ : I have a list and a Map
. I need to check all the values in the "Map" against the "List", if the value is in Map BUT NOT IN the List then  i need to remove it 
    List<String> list = getRequiredList();
    Set<String> set = new HashSet<>(list)
    Map<String, String> map = getMap(); 

need some info on how to remove from the map if its not in List

A:

A neat thing about Maps is that the key set and value collection are both backed by the Map itself.  What this means is that removing a key from the key set effectively removes it from the Map it belongs to.
Therefore, you can use retainAll on the key set:
map.keySet().retainAll(list);

That will make sure that the only keys in the Map are the Strings which are in your List.

