如何把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
如何把map的value转为list_如何在Java中将Map转换为List?相关推荐
- 把tensor转为numpy_如何在TensorFlow中将张量转换为numpy数组?
TensorFlow 2.0 急切执行默认情况下.numpy()处于启用状态,因此只需调用Tensor对象即可. import tensorflow as tf a = tf.constant([[1 ...
- scala hashmap_如何在Scala中将Hashmap转换为Map?
scala hashmap Let's first understand what are maps and hashmaps? 首先让我们了解什么是map和hashmap ? map in Scal ...
- Java中将Map转换为JSON
一个注意的地方:要选对jar包 Map map = new HashMap();map.put("success", "true");map.put(" ...
- Java中将Map转String,String转Map
暴力的直接Map对象toString()存,后面取出是就是用再转换为Map String转Map: JSONObject jsonobject = JSONObject.fromObject(str) ...
- java 转为Calendar_在Java中将String转换为Calendar对象
问题 我是Java新手,通常使用PHP. 我想转换这个字符串: 2011年3月14日16:02:37 GMT 2011 进入Calendar对象,这样我就可以像这样轻松地拉出Year和Month: S ...
- ibatis java.util.Map作为parameterClass和resultClass(转)
1.Map作为parameterClass 映射文件: Xml代码 <!--use Map type as parameterClass--> < select id = ...
- (转)ibatis Tips 之 java.util.Map作为parameterClass和resultClass
1.Map作为parameterClass 映射文件: Xml代码 <!--use Map type as parameterClass--> < select id = &qu ...
- 可以设置过期时间的Java缓存Map
前言 最近项目需求需要一个类似于redis可以设置过期时间的K,V存储方式.项目前期暂时不引进redis,暂时用java内存代替. 解决方案 1. ExpiringMap 功能简介 : 1.可设置Ma ...
- java的map键值类型是否固定_Java中Map根据键值(key)或者值(value)进行排序实现
我们都知道,java中的Map结构是key->value键值对存储的,而且根据Map的特性,同一个Map中 不存在两个Key相同的元素,而value不存在这个限制.换句话说,在同一个Map中Ke ...
- java中map转为json数据_Java技术-将java中Map类型数据转化为json数据并以Ajax形式返回...
Java技术-将java中Map类型数据转化为json数据并以Ajax形式返回html 1.自定义工具类(简单易用)-下面是我写的一个简单的工具类前端 package com.test.util; i ...
最新文章
- Servlet运行原理以及生命周期
- redis安装教程 windows环境
- Spring Boot快速开发企业级Admin管理后台
- 阿德莱德计算机科学怎么样,阿德莱德大学计算机科学硕士专业怎么样?成为IT大牛的不二之选...
- MFC实现最小化到托盘
- 微信小程序有这么几种常见的创业方式
- JavaScript prompt函数
- dataGridView相关问题
- Halcon教程系列/启蒙----(1)初识Halcon
- 牛!各大公司【薪资待遇】一览,我心动了
- 【物理应用】大气辐射和透射率模型及太阳和月亮模型(Matlab代码实现)
- 基于深度学习的车辆信息识别(一):车辆颜色识别
- 当“国风综艺”遇上“数字金融”,看国有大行解锁营销新范式
- 【组合数学】递推方程 ( 特解形式 | 特解求法 | 特解示例 )
- Hadoop文件系统
- Rainbow Brackets彩虹括号插件(简明安装)
- android 计算图片大小
- python 判断txt文件的编码格式
- php卡片式,卡片式设计的优点和不适用性
- Android 多启动图标icon,多启动页面