开发者

Get Set of entries from a Map

开发者 https://www.devze.com 2023-04-06 12:47 出处:网络
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?D开发者_运

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? D开发者_运维百科oing .entrySet() doesn't seem to work.


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()


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消