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");
此时stooges中有有三个元素。注意:此时的list不能进行add操作,否则会报“java.lang.UnsupportedOperationException”,Arrays.asList()返回的是List,而且是一个定长的List,所以不能转换为ArrayList,只能转换为AbstractList
原因在于asList()方法返回的是某个数组的列表形式,返回的列表只是数组的另一个视图,而数组本身并没有消失,对列表的任何操作最终都反映在数组上. 所以不支持remove,add方法的
- String[] arr = {"1", "2"};
- List list = Arrays.asList(arr);
- String[] arr = {"1", "2"};
- List list = Arrays.asList(arr);
4.数组转为set
- int[] a = { 1, 2, 3 };
-
- Set set = new HashSet(Arrays.asList(a));
- 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());
-
-
- List list = new ArrayList(map.values());
-
- System.out.println(list);
-
-
- Set set = new HashSet(map.values());
-
- System.out.println(set);
-
- 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());
-
-
- List list = new ArrayList(map.values());
-
- System.out.println(list);
-
-
- Set set = new HashSet(map.values());
-
- System.out.println(set);
6.list转数组
- List list = Arrays.asList("a","b");
-
- System.out.println(list);
-
-
-
- String[] arr = (String[])list.toArray(new String[list.size()]);
-
- System.out.println(Arrays.toString(arr));
java list map set array 转换
原文:http://www.cnblogs.com/xxsl/p/7224070.html