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

2025-05-29 0 51

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

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

1.List转换为Array

?

1

2

3

4

5

6
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

?

1

2
String[] countries = {"China", "Switzerland", "Italy", "France"};

List list = Arrays.asList(countries);

3.Map转换为List

?

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

4.Array转换为Set

?

1

2

3
String [] countries = {"India", "Switzerland", "Italy"};

Set<String> set = new HashSet<String>(Arrays.asList(countries));

System.out.println(set);

5.Map转换为Set

?

1

2
Map<Integer, String> sourceMap = createMap();

Set<String> targetSet = new HashSet<>(sourceMap.values());

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

原文链接:http://blog.csdn.net/suifeng3051/article/details/41699033

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 java中List、Array、Map、Set等集合相互转换 https://www.kuaiidc.com/116397.html

相关文章

发表评论
暂无评论