Search code examples
javahashmapset

Java: convert HashMap values to Set<Integer>


One more question about HashMap<> in Java:

I have the following

Map<String, Set<Integer>> myWordDict = new HashMap<String, Set<Integer>>();

After storing data into the variable myWordDict, I want to iterate through the HashMapValues, and add each value to a new Set variable?

When I try to do Set<Integer> newVariable = myWordDict.entrySet(), it seems the data type is incompatible.

So my question is essentially:

how to convert HashMap values or entrySet() to Set ?

Thanks


Solution

  • Your declaration should be like below. It will convert your map values to Collection

      Collection<Set<Integer>> newVariable = myWordDict.values();