从地图获取条目集

joh*_*ohn 3 java collections map

给出如下地图:

Map<String, Integer> = new Hashmap<String, Integer>;
Run Code Online (Sandbox Code Playgroud)

我怎样才能获得Collection<Integer>entrySet的任何(Collection的任何实现)?做.entrySet()似乎不起作用.

hvg*_*des 13

如果您只想获取地图值,可以使用该values()方法.Javadoc页面在这里.

这是因为您的需求是整数集合,并且映射值是Integer类型.

entrySet返回一个集合Map.Entry,其中每个实例都包含构成条目的键和值,因此如果您同时需要键和值,请使用entrySet()如此

Set<Map.Entry<String, Integer>> entries = map.entrySet()


Joh*_*n B 6

这取决于你是否真的想要一个SET.如果你想要一个真正的Set,你必须做:

Set mySet = new HashSet(map.values());
Run Code Online (Sandbox Code Playgroud)

请注意,给出了一个可以有重复条目的Collection.