简体   繁体   English

从地图获取条目集

[英]Get Set of entries from a Map

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? 如何获得entrySet的Collection<Integer> (Collection的任何实现)? Doing .entrySet() doesn't seem to work. 执行.entrySet()似乎不起作用。

If you want to get just the map values you can use the values() method. 如果只想获取地图值,可以使用values()方法。 The Javadoc page is here . Javadoc页面在这里

This is because your requirement is a Collection of Integers and the map values are of Integer type. 这是因为您的需求是整数集合,并且映射值是Integer类型。

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 entrySet返回Map.Entry的集合,其中每个实例都包含组成条目的键和值,因此如果您同时需要键和值,请使用entrySet() ,如此

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

That depends on if you truly want a SET. 这取决于你是否真的想要一个SET。 If you want a true Set you must do: 如果你想要一个真正的Set,你必须做:

Set mySet = new HashSet(map.values());

Notice that values gives a Collection which can have duplicate entries. 请注意, 给出了一个可以有重复条目的Collection。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM