转自:http://blog.csdn.net/top_code/article/details/10552827
1 package com.example.test; 2 3 import java.util.ArrayList; 4 import java.util.Arrays; 5 import java.util.HashMap; 6 import java.util.HashSet; 7 import java.util.List; 8 import java.util.Map; 9 import java.util.Set; 10 11 public class ConvertorTest { 12 13 /** 14 * @param args 15 */ 16 public static void main(String[] args) { 17 18 testList2Array(); 19 testArray2List(); 20 testSet2List(); 21 testList2Set(); 22 testSet2Array(); 23 testArray2Set(); 24 testMap2Set(); 25 testMap2List(); 26 } 27 28 private static void testMap2List() { 29 30 Map<String, String> map = new HashMap<String, String>(); 31 map.put("A", "ABC"); 32 map.put("K", "KK"); 33 map.put("L", "LV"); 34 35 // 将Map Key 转化为List 36 List<String> mapKeyList = new ArrayList<String>(map.keySet()); 37 System.out.println("mapKeyList:"+mapKeyList); 38 39 // 将Map Key 转化为List 40 List<String> mapValuesList = new ArrayList<String>(map.values()); 41 System.out.println("mapValuesList:"+mapValuesList); 42 43 } 44 45 private static void testMap2Set() { 46 47 Map<String, String> map = new HashMap<String, String>(); 48 map.put("A", "ABC"); 49 map.put("K", "KK"); 50 map.put("L", "LV"); 51 52 // 将Map 的键转化为Set 53 Set<String> mapKeySet = map.keySet(); 54 System.out.println("mapKeySet:"+mapKeySet); 55 56 // 将Map 的值转化为Set 57 Set<String> mapValuesSet = new HashSet<String>(map.values()); 58 System.out.println("mapValuesSet:"+mapValuesSet); 59 } 60 61 private static void testArray2Set() { 62 63 String[] arr = {"AA","BB","DD","CC","BB"}; 64 65 //数组-->Set 66 Set<String> set = new HashSet<String>(Arrays.asList(arr)); 67 System.out.println(set); 68 } 69 70 private static void testSet2Array() { 71 Set<String> set = new HashSet<String>(); 72 set.add("AA"); 73 set.add("BB"); 74 set.add("CC"); 75 76 String[] arr = new String[set.size()]; 77 //Set-->数组 78 set.toArray(arr); 79 System.out.println(Arrays.toString(arr)); 80 } 81 82 private static void testList2Set() { 83 84 List<String> list = new ArrayList<String>(); 85 list.add("ABC"); 86 list.add("EFG"); 87 list.add("LMN"); 88 list.add("LMN"); 89 90 //List-->Set 91 Set<String> listSet = new HashSet<String>(list); 92 System.out.println(listSet); 93 } 94 95 private static void testSet2List() { 96 97 Set<String> set = new HashSet<String>(); 98 set.add("AA"); 99 set.add("BB"); 100 set.add("CC"); 101 102 //Set --> List 103 List<String> setList = new ArrayList<String>(set); 104 System.out.println(setList); 105 } 106 107 private static void testList2Array() { 108 //List-->数组 109 List<String> list = new ArrayList<String>(); 110 list.add("AA"); 111 list.add("BB"); 112 list.add("CC"); 113 Object[] objects = list.toArray();//返回Object数组 114 System.out.println("objects:"+Arrays.toString(objects)); 115 116 String[] arr = new String[list.size()]; 117 list.toArray(arr);//将转化后的数组放入已经创建好的对象中 118 System.out.println("strings1:"+Arrays.toString(arr)); 119 } 120 121 private static void testArray2List() { 122 //数组-->List 123 String[] ss = {"JJ","KK"}; 124 List<String> list1 = Arrays.asList(ss); 125 List<String> list2 = Arrays.asList("AAA","BBB"); 126 System.out.println(list1); 127 System.out.println(list2); 128 } 129 130 }
时间: 2024-10-28 15:36:01