转换HashMap 至 数组列表 in 爪哇
在这篇文章中,我们将看到如何转换 哈希图 至 数组列表 在Java中。很多时候,您需要将键或值存储到ArrayList或存储HashMap中’s Node objects in 数组列表.
目录
哈希图 和ArrayList都是最常用的 爪哇 中的数据结构 两者都有不同的层次结构。
这是将HashMap转换为ArrayList的方法 爪哇 .
使用Java 8’s Stream API
爪哇 8: 转换HashMap’s keys 至 数组列表
我们可以使用HashMap’s 键集 () 获取一组密钥并使用的方法 爪哇 8‘的Stream API将其转换为ArrayList。
1 2 3 4 5 |
清单 <Integer> customerIdList = customerIdNameMap. 键集 () . 流 () . 收藏 (收藏家. 到清单 ()); |
转换HashMap’s的值到ArrayList
我们可以使用HashMap’s 价值观 ()
method 至 get a 收藏 ion of 价值观 and use 爪哇 8’的Stream API将其转换为ArrayList。
1 2 3 4 5 |
清单 客户名称 = customerIdNameMap. 价值观 () . 流 () . 收藏 (收藏家. 到清单 ()); |
转换HashMap’s 数组列表的入口对象
我们可以用 哈希图 ’s entrySet ()
获取Entry对象集并使用Java 8的方法’的Stream API将其转换为 数组列表.
1 2 3 4 5 |
清单 < 条目 < 整数 , 串 >> entryCustomerList = customerIdNameMap.entrySet () . 流 () . 收藏 (收藏家. 到清单 ()); |
让’请参阅完整的示例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
包 组织 . Arpit . 爪哇 2blog; 进口 爪哇 . 实用程序 .数组列表; 进口 爪哇 . 实用程序 .采集; 进口 爪哇 . 实用程序 . 哈希图 ; 进口 爪哇 . 实用程序 . 清单 ; 进口 爪哇 . 实用程序 . 地图 ; 进口 爪哇 . 实用程序 . 地图 . 条目 ; 进口 爪哇 . 实用程序 . 组 ; 进口 爪哇 . 实用程序 . 流 .收藏家; 上市 类 哈希图 ToArrayListMainJava8 { 上市 静态的 虚空 主要 ( 串 [] args ) { 哈希图 < 整数 , 串 > customerIdNameMap = 新 哈希图 < 整数 , 串 >(); //将键值对放入HashMap customerIdNameMap. 放 (1001, “阿曼” ); customerIdNameMap. 放 (1002, “贾文” ); customerIdNameMap. 放 (1003, “垫” ); customerIdNameMap. 放 (1004, “乔” ); // 爪哇 8 //将键转换为ArrayList 清单 < 整数 > customerIdList = customerIdNameMap. 键集 () . 流 () . 收藏 (收藏家. 到清单 ()); 系统 . 出 . 打印 (“ customerIds:”+customerIdList); //将值转换为ArrayList 清单 < 串 > 客户名称 = customerIdNameMap. 价值观 () . 流 () . 收藏 (收藏家. 到清单 ()); 系统 . 出 . 打印 (“客户名称:”+ 客户名称); //将入口对象转换为ArrayList 清单 < 条目 < 整数 , 串 >> entryCustomerList = customerIdNameMap.entrySet () . 流 () . 收藏 (收藏家. 到清单 ()); 系统 . 出 . 打印 (“客户ID和名称:”+entryCustomerList); } } |
输出:
客户名称:[Arman,Javin,Mat,Joe]
客户ID和名称:[1001 = Arman,1002 = Javin,1003 = Mat,1004 = Joe]
使用ArrayList’s 建设者
转换HashMap’s keys 至 数组列表
得到 键集 () 从HashMap,然后使用其将其转换为ArrayList 建设者.
1 2 3 4 5 |
//将键转换为ArrayList 组 键集 = customerIdNameMap. 键集 (); 清单 customerIdList = 新 数组列表( 键集 ); |
转换HashMap’s的值到ArrayList
得到 价值观 () 从HashMap,然后使用其构造函数将其转换为ArrayList。
1 2 3 4 5 |
//将值转换为ArrayList 采集 价值观 = customerIdNameMap. 价值观 (); 清单 客户名称 = 新 数组列表( 价值观 ); |
转换HashMap’s 数组列表的入口对象
得到 entrySet () 从HashMap,然后使用其构造函数将其转换为ArrayList。
1 2 3 4 5 |
//将入口对象转换为ArrayList 组 < 条目 < 整数 , 串 >> entrySet = customerIdNameMap.entrySet (); 清单 < 地图 . 条目 < 整数 , 串 >> entryCustomerList = 新 数组列表< 地图 . 条目 < 整数 , 串 >>(entrySet ); |
这是将HashMap转换为ArrayList的完整程序。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
包 组织 . Arpit . 爪哇 2blog; 进口 爪哇 . 实用程序 .数组列表; 进口 爪哇 . 实用程序 .采集; 进口 爪哇 . 实用程序 . 哈希图 ; 进口 爪哇 . 实用程序 . 清单 ; 进口 爪哇 . 实用程序 . 地图 ; 进口 爪哇 . 实用程序 . 地图 . 条目 ; 进口 爪哇 . 实用程序 . 组 ; 上市 类 哈希图 ToArrayListMain { 上市 静态的 虚空 主要 ( 串 [] args ) { 哈希图 < 整数 , 串 > customerIdNameMap = 新 哈希图 < 整数 , 串 >(); //将键值对放入HashMap customerIdNameMap. 放 (1001, “阿曼” ); customerIdNameMap. 放 (1002, “贾文” ); customerIdNameMap. 放 (1003, “垫” ); customerIdNameMap. 放 (1004, “乔” ); //在Java 8之前 //将键转换为ArrayList 组 < 整数 > 键集 = customerIdNameMap. 键集 (); 清单 < 整数 > customerIdList = 新 数组列表< 整数 >( 键集 ); 系统 . 出 . 打印 (“ customerIds:”+customerIdList); //将值转换为ArrayList 采集< 串 > 价值观 = customerIdNameMap. 价值观 (); 清单 < 串 > 客户名称 = 新 数组列表< 串 >( 价值观 ); 系统 . 出 . 打印 (“客户名称:”+ 客户名称); //将入口对象转换为ArrayList 组 < 条目 < 整数 , 串 >> entrySet = customerIdNameMap.entrySet (); 清单 < 地图 . 条目 < 整数 , 串 >> entryCustomerList = 新 数组列表< 地图 . 条目 < 整数 , 串 >>(entrySet ); 系统 . 出 . 打印 (“客户ID和名称:”+entryCustomerList); } } |
爪哇 哈希图 教程
- 爪哇 中的HashMap
- 哈希图 内部工作
- 哈希图 中的hash和indexfor方法
- 爪哇 中的哈希码和等于 分类
- 通过键和值的HashMap
- 哈希图 和HashSet之间的区别
- 哈希图 和Hashtable之间的区别
- 如何遍历HashMap