Get Set of entries from a Map
12,671
Solution 1
If you want to get just the map values you can use the values()
method. The Javadoc page is here.
This is because your requirement is a Collection of Integers and the map values are of Integer type.
entrySet
returns a collection of Map.Entry
, each instance of which contains both the key and value that make up the entry, so if you want both the key and value, use entrySet()
like so
Set<Map.Entry<String, Integer>> entries = map.entrySet()
Solution 2
That depends on if you truly want a SET. If you want a true Set you must do:
Set mySet = new HashSet(map.values());
Notice that values gives a Collection which can have duplicate entries.
Author by
Admin
Updated on June 13, 2022Comments
-
Admin almost 2 years
Given a map such as:
Map<String, Integer> = new Hashmap<String, Integer>;
How can I get a
Collection<Integer>
(any implementation of Collection would do) of the entrySet? Doing.entrySet()
doesn't seem to work.