欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

java中List、Array、Map、Set等集合相互转换

时间:2021-06-14 09:43:22|栏目:JAVA代码|点击:

java中List、Array、Map、Set等集合相互转换

  在java中,我们经常需要对List、Array等做一些转换操作,当然转换方法有很多种,但哪种方法既方便又高效呢?在这里向大家介绍一下集合间的最佳转换方法。

1.List转换为Array

List<String> list = new ArrayList<String>(); 
list.add("China"); 
list.add("Switzerland"); 
list.add("Italy"); 
list.add("France"); 
String [] countries = list.toArray(new String[list.size()]); 

2.Array转换为List

String[] countries = {"China", "Switzerland", "Italy", "France"}; 
List list = Arrays.asList(countries); 

3.Map转换为List

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

4.Array转换为Set

String [] countries = {"India", "Switzerland", "Italy"};    
Set<String> set = new HashSet<String>(Arrays.asList(countries)); 
System.out.println(set); 

5.Map转换为Set

Map<Integer, String> sourceMap = createMap(); 
Set<String> targetSet = new HashSet<>(sourceMap.values()); 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:深入理解spring多数据源配置

栏    目:JAVA代码

下一篇:解决eclipse上传svn忽略target文件夹的坑

本文标题:java中List、Array、Map、Set等集合相互转换

本文地址:http://www.codeinn.net/misctech/141479.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有