Java集合非常的重要,尤其在业务中,如果你在熟练的使用Java数据结果的集合工作,将会大大的提高工作效率,减少代码量。

 

1.集合的互换

1.1 Map转Set

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

1.2 Map转List

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

1.3 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()]);

1.4 Array转List

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

1.5 Array转Set

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

posted on 2016-09-06 10:54  yanweiqi  阅读(232)  评论(0编辑  收藏  举报