list、set、map、array间的相互转换
- list转set
Set set = new HashSet(new ArrayList());
- set转list
List list = new ArrayList(new HashSet());
- array转为list
List stooges = Arrays.asList("Larry", "Moe", "Curly"); 或者 String[] arr = {"1", "2"}; List list = Arrays.asList(arr);
- array转为set
int[] a = { 1, 2, 3 }; Set set = new HashSet(Arrays.asList(a));
- 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);
- list转array
List list = Arrays.asList("a","b"); String[] arr = (String[])list.toArray(new String[list.size()]); System.out.println(Arrays.toString(arr));