码迷,mamicode.com
首页 > 编程语言 > 详细

java中List、Array、Map、Set等集合相互转换的最佳方法

时间:2014-12-03 17:14:45      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:java 集合转换   list转换为array   

     在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());


java中List、Array、Map、Set等集合相互转换的最佳方法

标签:java 集合转换   list转换为array   

原文地址:http://blog.csdn.net/suifeng3051/article/details/41699033

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!