如何在 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
How to convert a Map to List in Java?
提问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 map
is your instance of Map
假设map
是你的实例Map
map.values()
will return aCollection
containing all of the map's values.map.keySet()
will return aSet
containing all of the map's keys.
map.values()
将返回Collection
包含所有地图值的 。map.keySet()
将返回一个Set
包含所有地图键的。
回答by coobird
The issue here is that Map
has two values (a key and value), while a List
only has one value (an element).
这里的问题是它Map
有两个值(一个键和一个值),而一个List
只有一个值(一个元素)。
Therefore, the best that can be done is to either get a List
of 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 List
can be obtained by creating a new ArrayList
from a Set
returned by the Map.keySet
method:
List
可以通过ArrayList
从方法Set
返回的a 中创建一个 new 来获取a 的键Map.keySet
:
List<String> list = new ArrayList<String>(m.keySet());
While the values as a List
can be obtained creating a new ArrayList
from a Collection
returned by the Map.values
method:
虽然List
可以ArrayList
从方法Collection
返回的a创建新的 a获得值Map.values
:
List<String> list = new ArrayList<String>(m.values());
The result of getting the List
of keys:
获取List
密钥的结果:
Apple Another Hello
The result of getting the List
of values:
获取List
值的结果:
3.14 Element World
回答by maneesh
I guess you want to convert the values contained in the Map
to a list
? Easiest is to call the values()
method of the Map
interface. This will return the Collection
of value objects contained in the Map
.
我猜您想将包含在中的值转换Map
为list
? 最简单的就是调用接口的values()
方法Map
。这将返回Collection
包含在Map
.
Note that this Collection
is backed by the Map
object and any changes to the Map
object will reflect here. So if you want a separate copy not bound to your Map
object, simply create a new List
object like an ArrayList
passing the value Collection
as 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.Entry
objects 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" SortedMap
somehow.
如果要确保结果中的值在List<Value>
input 的键排序中,则Map<Key, Value>
需要以SortedMap
某种方式“通过” 。
Either startwith a concrete SortedMap
implementation (Such as TreeMap
) or insert your input Map
into a SortedMap
before 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 Map
implementation provides, which can often be something other than the natural key ordering (Try Hashtable
or ConcurrentHashMap
, for variety).
否则,您将获得Map
实现提供的任何本机排序,这通常可能是自然键排序以外的其他内容(尝试Hashtable
或ConcurrentHashMap
, 表示多样性)。
回答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());
} "