如何在 Java 中将地图转换为列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1026723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:23:47  来源:igfitidea点击:

How to convert a Map to List in Java?

javalistcollectionsmap

提问by Javaa

What is the best way to convert a Map<key,value>to a List<value>? Just iterate over all values and insert them in a list or am I overlooking something?

将 a 转换Map<key,value>为 a的最佳方法是List<value>什么?只需遍历所有值并将它们插入列表中,还是我忽略了什么?

采纳答案by cletus

List<Value> list = new ArrayList<Value>(map.values());

assuming:

假设:

Map<Key,Value> map;

回答by Diego Amicabile

a list of what ?

什么清单?

Assuming mapis your instance of Map

假设map是你的实例Map

  • map.values()will return a Collectioncontaining all of the map's values.
  • map.keySet()will return a Setcontaining all of the map's keys.
  • map.values()将返回Collection包含所有地图值的 。
  • map.keySet()将返回一个Set包含所有地图键的。

回答by coobird

The issue here is that Maphas two values (a key and value), while a Listonly has one value (an element).

这里的问题是它Map有两个值(一个键和一个值),而一个List只有一个值(一个元素)。

Therefore, the best that can be done is to either get a Listof the keys or the values. (Unless we make a wrapper to hold on to the key/value pair).

因此,可以做的最好的事情是获取一个List键或值。(除非我们制作一个包装器来保持键/值对)。

Say we have a Map:

假设我们有一个Map

Map<String, String> m = new HashMap<String, String>();
m.put("Hello", "World");
m.put("Apple", "3.14");
m.put("Another", "Element");

The keys as a Listcan be obtained by creating a new ArrayListfrom a Setreturned by the Map.keySetmethod:

List可以通过ArrayList从方法Set返回的a 中创建一个 new 来获取a 的键Map.keySet

List<String> list = new ArrayList<String>(m.keySet());

While the values as a Listcan be obtained creating a new ArrayListfrom a Collectionreturned by the Map.valuesmethod:

虽然List可以ArrayList从方法Collection返回的a创建新的 a获得值Map.values

List<String> list = new ArrayList<String>(m.values());

The result of getting the Listof keys:

获取List密钥的结果:

Apple
Another
Hello

The result of getting the Listof values:

获取List值的结果:

3.14
Element
World

回答by maneesh

I guess you want to convert the values contained in the Mapto a list? Easiest is to call the values()method of the Mapinterface. This will return the Collectionof value objects contained in the Map.

我猜您想将包含在中的值转换Maplist? 最简单的就是调用接口的values()方法Map。这将返回Collection包含在Map.

Note that this Collectionis backed by the Mapobject and any changes to the Mapobject will reflect here. So if you want a separate copy not bound to your Mapobject, simply create a new Listobject like an ArrayListpassing the value Collectionas below.

请注意,这Collection是由Map对象支持的,对对象的任何更改Map都将反映在此处。因此,如果您想要一个不绑定到您的Map对象的单独副本,只需创建一个新List对象,例如ArrayList传递如下值Collection

ArrayList<String> list = new ArrayList<String>(map.values());

回答by java dude

map.entrySet()gives you a collection of Map.Entryobjects containing both key and value. you can then transform this into any collection object you like, such as new ArrayList(map.entrySet());

map.entrySet()为您提供Map.Entry包含键和值的对象集合。然后,您可以将其转换为您喜欢的任何集合对象,例如new ArrayList(map.entrySet())

回答by M0les

If you want to ensure the values in the resultant List<Value>are in the key-ordering of the input Map<Key, Value>, you need to "go via" SortedMapsomehow.

如果要确保结果中的值在List<Value>input 的键排序中,则Map<Key, Value>需要以SortedMap某种方式“通过” 。

Either startwith a concrete SortedMapimplementation (Such as TreeMap) or insert your input Mapinto a SortedMapbefore converting that to List. e.g.:

无论是开始一个具体的SortedMap实现(如TreeMap)或插入您的输入Map到一个SortedMap转换,为之前List。例如:

Map<Key,Value> map;
List<Value> list = new ArrayList<Value>( new TreeMap<Key Value>( map ));

Otherwise you'll get whatever native ordering the Mapimplementation provides, which can often be something other than the natural key ordering (Try Hashtableor ConcurrentHashMap, for variety).

否则,您将获得Map实现提供的任何本机排序,这通常可能是自然键排序以外的其他内容(尝试HashtableConcurrentHashMap, 表示多样性)。

回答by user11

    Map<String, Integer> map = new HashMap<String, Integer>();
    map.put("java", 20);
    map.put("C++", 45);

    Set <Entry<String, Integer>> set = map.entrySet();

    List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set);

we can have both key and value pair in list.Also can get key and value using Map.Entry by iterating over list.

我们可以在列表中同时拥有键和值对。也可以通过遍历列表使用 Map.Entry 获取键和值。

回答by user3752441

You can do it like this

你可以这样做

List<Value> list = new ArrayList<Value>(map.values());

回答by Matej Kormuth

Using the Java 8 Streams API.

使用 Java 8 Streams API。

List<Value> values = map.values().stream().collect(Collectors.toList());

回答by siva prasad

"Map<String , String > map = new HapshMap<String , String>;
 map.add("one","java");
 map.add("two" ,"spring");
 Set<Entry<String,String>> set =  map.entrySet();
 List<Entry<String , String>> list = new ArrayList<Entry<String , String>>    (set);
 for(Entry<String , String> entry : list ) {
   System.out.println(entry.getKey());
   System.out.println(entry.getValue());
 } "