首页 > 代码库 > java中如何实现数组、List、Set、Map之间互相转换。
java中如何实现数组、List、Set、Map之间互相转换。
1、数组转List:
String[] countries = {"Array", "List", "Set", "Map"};
List list = Arrays.asList(countries);
2、Array转:Set
String [] countries = {"Array", "List", "Set"};
Set<String> set = new HashSet<String>(Arrays.asList(countries));
System.out.println(set);
3、List转Array:
List<String> list = new ArrayList<String>();
list.add("Array");
list.add("List");
list.add("Set");
list.add("Map");
String [] countries = list.toArray(new String[list.size()]);
4、Map转List:
List<Value> list = new ArrayList<Value>(map.values());
5、Map转Set:
Map<Integer, String> map = createMap();
Set<String> set = new HashSet<>(sourceMap.values());
6、List转Set:
String[] staffs
=
new String[]{"Array", "List", "Set"};
List staffsList = Arrays.asList(staffs);
Set result = new HashSet(staffsList);
java中如何实现数组、List、Set、Map之间互相转换。