简体   繁体   English

如何创建列表<map.entry<string, integer> > 出一个 Map<string, integer> ? </string,></map.entry<string,>

[英]How do I create a List<Map.Entry<String, Integer>> out of a Map<String, Integer>?

So I know how to create a List of Map values new ArrayList<String, Integer>(map.getValues());所以我知道如何创建 Map 值的列表new ArrayList<String, Integer>(map.getValues()); Or from entry set map.entrySet() However there doesn't seem to be a similar way to add a list of Map.Entry<String, Integer> Is there a way to do this so I can write it all in just a return statement?或者从条目集map.entrySet()但是似乎没有类似的方法来添加 Map.Entry<String, Integer> 的列表有没有办法做到这一点所以我可以把它全部写下来陈述?

Map<String, Integer> map = new TreeMap<String, Integer>();

public List<Map.Entry<String, Integer>> getWordList(){
        return new ArrayList<Map.Entry<String, Integer>>(map);
}

This doesn't work.这是行不通的。 But what does?但是呢?

entrySet returns a Set and not a List , which means that the map's keys may or may not be in order. entrySet返回一个Set而不是List ,这意味着地图的键可能有序也可能不有序。

However, in the case of TreeMap the JavaDoc says the following about it:然而,在TreeMap的情况下, JavaDoc 说了以下内容:

Returns a Set view of the mappings contained in this map.返回此 map 中包含的映射的Set视图。

The set's iterator returns the entries in ascending key order.该集合的迭代器以升序键顺序返回条目。

Apparently, entrySet() guarantees the mappings to be in the same order as the TreeMap .显然, entrySet()保证映射与TreeMap的顺序相同。 In short, you could just use it anywhere where a Collection is required.简而言之,您可以在需要Collection的任何地方使用它。 Creating a List is then trivial:创建一个List就很简单了:

new ArrayList<>(map.entrySet())

or或者

List.copyOf(map.entrySet())

暂无
暂无

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

相关问题 排序列表<map.entry<string, integer> &gt; 主要和次要方式? </map.entry<string,> - Sort List<Map.Entry<String, Integer>> in primary and secondary way? 无法从 Map.Entry 转换<String,Integer>到字符串 - Cannot convert from Map.Entry<String,Integer> to String 如何转换列表<namevaluepair>进入列表<map.entry<string, string> &gt;? </map.entry<string,></namevaluepair> - How to convert List<NameValuePair> into a List<Map.Entry<String, String>>? 优先队列<Map.Entry<String, Integer> &gt; 不接受来自我的地图的条目对象 - PriorityQueue<Map.Entry<String, Integer>> not accepting entry objects from my map 如何从Map.Entry进行投射 <Integer,Float> Java中转换为Integer - How can I cast from Map.Entry<Integer,Float> to Integer in Java 如何获取列表的值 <Map<Integer, String> &gt;? - How do I get the values of the List<Map<Integer, String>>? 如何将具有整数值的条目添加到 Map<String, ?> - How to add an entry with an Integer value into Map<String, ?> 如何转换地图 <Integer, List<String> &gt;到地图 <Integer, Set<String> &gt; - how to convert Map<Integer, List<String>> to Map<Integer, Set<String>> 如何遍历具有价值的地图Map <String,Integer> 声明为地图 <String,Map<String,Integer> &gt;使用表达语言? - How do I iterate through a map that has value, Map<String,Integer> declared as Map<String,Map<String,Integer>>using Expression Language ? 使用Java 8流,如何转换Map <String, Map<String, List<Person> &gt;&gt;到地图 <Integer, Map<String, Integer> &gt;? - Using Java 8 streams, how to transform Map<String, Map<String, List<Person>>> to Map<Integer, Map<String, Integer>>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM