下面代码演示了List<-->数组、List<-->Set、数组<-->Set、Map将键转化为Set、Map将值转化为Set、Map将值转化为List等集合常用转化操作。
public class TestApp { public static void main(String[] args) { //List-->数组 Listlist = new ArrayList (); list.add("蹇伟"); list.add("Jerval"); list.add("杰威"); Object[] objects = list.toArray();//返回Object数组 System.out.println("objects:"+Arrays.toString(objects)); String[] strings1 = new String[list.size()]; list.toArray(strings1);//将转化后的数组放入已经创建好的对象中 System.out.println("strings1:"+Arrays.toString(strings1)); String[] strings2 = list.toArray(new String[0]);//将转化后的数组赋给新对象 System.out.println("strings2:"+Arrays.toString(strings2)); //数组-->List String[] ss = {"JJ","KK"}; List list1 = Arrays.asList(ss); List list2 = Arrays.asList("AAA","BBB"); System.out.println(list1); System.out.println(list2); //List-->Set List list3 = new ArrayList (new HashSet ()); //Set-->List Set set = new HashSet (new ArrayList ()); //数组-->Set String[] strs = {"AA","BB"}; Set set2 = new HashSet (Arrays.asList(strs)); System.out.println(set2); //Set-->数组 Set set3 = new HashSet (Arrays.asList("PP","OO")); String[] strSet = new String[set3.size()]; set3.toArray(strSet); System.out.println(Arrays.toString(strSet)); //Map操作 Map map = new HashMap (); map.put("YYY", "UUU"); map.put("RRR", "TTT"); // 将键转化为Set Set mapKeySet = map.keySet(); // 将值转化为Set Set mapValuesSet = new HashSet (map.values()); // 将值转化为List List mapValuesList = new ArrayList (map.values()); }}