Java 集合转换(数组、List、Set、Map相互转换)
list,set,map,数组间的相互转换
1.list转set
Set set = new HashSet(new ArrayList());
2.set转list
List list = new ArrayList(new HashSet());
3.数组转为list
List stooges = Arrays.asList("Larry", "Moe", "Curly");
或者
String[] arr = {"1", "2"};
List list = Arrays.asList(arr);
4.数组转为set
int[] a = { 1, 2, 3 };
Set set = new HashSet(Arrays.asList(a));
5.map的相关操作。
Map map = new HashMap();
map.put("1", "a");
map.put('2', 'b');
map.put('3', 'c');
System.out.println(map);
// 输出所有的值
System.out.println(map.keySet());
// 输出所有的键
System.out.println(map.values());
// 将map的值转化为List
List list = new ArrayList(map.values());
System.out.println(list);
// 将map的值转化为Set
Set set = new HashSet(map.values());
System.out.println(set);
6.list转数组
List list = Arrays.asList("a","b");
String[] arr = (String[])list.toArray(new String[list.size()]);
System.out.println(Arrays.toString(arr));
public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); map.put("1", "value1"); map.put("2", "value2"); map.put("3", "value3"); //第一种:普遍使用,二次取值 System.out.println("通过Map.keySet遍历key和value:"); for (String key : map.keySet()) { System.out.println("key= "+ key + " and value= " + map.get(key)); } //第二种 System.out.println("通过Map.entrySet使用iterator遍历key和value:"); Iterator<Map.Entry<String, String>> it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, String> entry = it.next(); System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); } //第三种:推荐,尤其是容量大时 System.out.println("通过Map.entrySet遍历key和value"); for (Map.Entry<String, String> entry : map.entrySet()) { System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); } //第四种 System.out.println("通过Map.values()遍历所有的value,但不能遍历key"); for (String v : map.values()) { System.out.println("value= " + v); } }
还可以进行如下的操作
package com.example.test; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; public class ConvertorTest { /** * @param args */ public static void main(String[] args) { testList2Array(); testArray2List(); testSet2List(); testList2Set(); testSet2Array(); testArray2Set(); testMap2Set(); testMap2List(); } private static void testMap2List() { Map<String, String> map = new HashMap<String, String>(); map.put("A", "ABC"); map.put("K", "KK"); map.put("L", "LV"); // 将Map Key 转化为List List<String> mapKeyList = new ArrayList<String>(map.keySet()); System.out.println("mapKeyList:"+mapKeyList); // 将Map Key 转化为List List<String> mapValuesList = new ArrayList<String>(map.values()); System.out.println("mapValuesList:"+mapValuesList); } private static void testMap2Set() { Map<String, String> map = new HashMap<String, String>(); map.put("A", "ABC"); map.put("K", "KK"); map.put("L", "LV"); // 将Map 的键转化为Set Set<String> mapKeySet = map.keySet(); System.out.println("mapKeySet:"+mapKeySet); // 将Map 的值转化为Set Set<String> mapValuesSet = new HashSet<String>(map.values()); System.out.println("mapValuesSet:"+mapValuesSet); } private static void testArray2Set() { String[] arr = {"AA","BB","DD","CC","BB"}; //数组-->Set Set<String> set = new HashSet<String>(Arrays.asList(arr)); System.out.println(set); } private static void testSet2Array() { Set<String> set = new HashSet<String>(); set.add("AA"); set.add("BB"); set.add("CC"); String[] arr = new String[set.size()]; //Set-->数组 set.toArray(arr); System.out.println(Arrays.toString(arr)); } private static void testList2Set() { List<String> list = new ArrayList<String>(); list.add("ABC"); list.add("EFG"); list.add("LMN"); list.add("LMN"); //List-->Set Set<String> listSet = new HashSet<String>(list); System.out.println(listSet); } private static void testSet2List() { Set<String> set = new HashSet<String>(); set.add("AA"); set.add("BB"); set.add("CC"); //Set --> List List<String> setList = new ArrayList<String>(set); System.out.println(setList); } private static void testList2Array() { //List-->数组 List<String> list = new ArrayList<String>(); list.add("AA"); list.add("BB"); list.add("CC"); Object[] objects = list.toArray();//返回Object数组 System.out.println("objects:"+Arrays.toString(objects)); String[] arr = new String[list.size()]; list.toArray(arr);//将转化后的数组放入已经创建好的对象中 System.out.println("strings1:"+Arrays.toString(arr)); } private static void testArray2List() { //数组-->List String[] ss = {"JJ","KK"}; List<String> list1 = Arrays.asList(ss); List<String> list2 = Arrays.asList("AAA","BBB"); System.out.println(list1); System.out.println(list2); } }