如何把map的value转为list_如何在Java中将Map转换为List?

如何在Java中将Map转换为List?

怎样把Map转换成List的最佳途径? 只是迭代所有值并将它们插入列表中或者我忽略了什么?

13个解决方案

1157 votes

List list = new ArrayList(map.values());

假设:

Map map;

cletus answered 2018-12-26T02:38:21Z

125 votes

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

因此,最好的办法是获得List的密钥或值。 (除非我们使用包装器来保持键/值对)。

假设我们有一个List:

Map m = new HashMap();

m.put("Hello", "World");

m.put("Apple", "3.14");

m.put("Another", "Element");

作为List的密钥可以通过从List方法返回的List创建新的List来获得:

List list = new ArrayList(m.keySet());

虽然可以通过List方法返回的List创建新的ArrayList,但值为List:

List list = new ArrayList(m.values());

获取List密钥的结果:

Apple

Another

Hello

获取List值的结果:

3.14

Element

World

coobird answered 2018-12-26T02:39:08Z

28 votes

使用Java 8 Streams API。

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

Matej Kormuth answered 2018-12-26T02:39:28Z

20 votes

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

java dude answered 2018-12-26T02:39:48Z

20 votes

什么的清单?

假设map.keySet()是Set的实例

map.keySet()将返回包含所有地图值的Set。

map.keySet()将返回包含所有地图键的Set。

Diego Amicabile answered 2018-12-26T02:40:21Z

12 votes

我想你想把Collection中包含的值转换为Map? 最简单的是调用Map接口的Map方法。 这将返回List中包含的值对象的List。

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

ArrayList list = new ArrayList(map.values());

maneesh answered 2018-12-26T02:40:47Z

6 votes

你可以这样做

List list = new ArrayList(map.values());

user3752441 answered 2018-12-26T02:41:07Z

4 votes

如果要确保结果Map中的值是输入Hashtable的键排序,则需要以某种方式“通过”ConcurrentHashMap。

从具体的Map实施开始(如Hashtable)或将输入ConcurrentHashMap插入SortedMap,然后再将其转换为List。例如:

Map map;

List list = new ArrayList( new TreeMap( map ));

否则,您将获得Map实现提供的任何本机订购,这通常不是自然键排序(尝试Hashtable或ConcurrentHashMap,用于多样化)。

M0les answered 2018-12-26T02:41:36Z

3 votes

Map map = new HashMap();

map.put("java", 20);

map.put("C++", 45);

Set > set = map.entrySet();

List> list = new ArrayList>(set);

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

user11 answered 2018-12-26T02:41:56Z

0 votes

"Map map = new HapshMap;

map.add("one","java");

map.add("two" ,"spring");

Set> set = map.entrySet();

List> list = new ArrayList> (set);

for(Entry entry : list ) {

System.out.println(entry.getKey());

System.out.println(entry.getValue());

} "

siva prasad answered 2018-12-26T02:42:12Z

0 votes

HashMap> map = new HashMap<>();

List list = new ArrayList();

list.add("Java");

list.add("Primefaces");

list.add("JSF");

map.put(1,list);

if(map != null){

return new ArrayList((Collection extends String>) map.values());

}

Hakan Anlamaz answered 2018-12-26T02:42:28Z

0 votes

// you can use this

List list = new ArrayList(map.values());

// or you may use

List list = new ArrayList();

for (Map.Entry entry : map.entrySet())

{

list.add(entry.getValue());

}

saurabhgoyal795 answered 2018-12-26T02:42:43Z

-1 votes

这是从map获取值的通用方法。

public static List ValueListFromMap(HashMap map) {

List thingList = new ArrayList<>();

for (Map.Entry entry : map.entrySet()) {

thingList.add(entry.getValue());

}

return thingList;

}

Hiral Pancholi answered 2018-12-26T02:43:03Z

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值