Skip to content
Advertisement

What is the purpose of placing a HashSet inside of a HashMap?

For example: private HashMap<Integer, HashSet> variableName;

I understand that HashMap implements Map and doesn’t allow duplicate keys and HashSet implements Set and doesn’t allow for duplicate values, but what is the purpose of placing a HashSet inside of a HashMap? Do they not achieve similar tasks by themselves (though in different ways and with different performance)? What functionality does doing this provide that a HashMap by itself couldn’t?

Advertisement

Answer

You do this when you want to map a key to more than one thing. A Map can only map a key to a single thing, but that single thing can be a Set which holds multiple things.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement