List、Set、Map、數組之間各類轉換

剛學Java不久的時候,接到一個電面,而後問了一些java的知識,好比說Java的編碼,Unicode等,可是最讓我蛋疼的是怎麼嗎map轉爲set,那個時候對集合用的不多,對集合不是特別瞭解,map還知道,set就蒙了,而後轉爲set更蒙了,以爲應該有API提供吧,可是不知道怎麼說。後來我一直下來再查這個問題,查到了,可是沒有實踐過,今天我就來一發代碼。java

List轉Set                                                                                    數組

Set set = new HashSet(new ArrayList());

Set轉List                                                                                    編碼

List list = new ArrayList(new HashSet());

數組轉爲List                                                                                 spa

List arr = Arrays.asList("1", "2", "3");
//或者
String[] arr = {"1", "2"};
List list = Arrays.asList(arr);

數組轉爲Set                                                                                  code

int[] arr = { 1, 2, 3 };
Set set = new HashSet(Arrays.asList(arr));

Map的值轉化爲List                                                                       blog

List list = new ArrayList(map.values());

Map的值轉化爲Set                                                                        three

Set set = new HashSet(map.values());

List轉數組                                                                                    ip

List list = Arrays.asList("a","b");
String[] arr = (String[])list.toArray(new String[list.size()]);

代碼                                                                                          get

public class listsetmao {

    private static List<String> arrayList;
    private static Map<String, String> hashMap;
    private static Set<String> hashSet;
    private static String[] arr = {"11oneone","22twotwo"};

    public static void main(String[] args) {
        /*
         * //list轉set initList(); Set<String> set = new
         * HashSet<String>(arrayList);
         * System.out.println("arrayList.toString()--->"+set.toString());
         * System.out.println("set.toString()--->"+set.toString());
         */
        /*
         * //set轉list initSet(); List<String> list = new
         * ArrayList<String>(hashSet);
         * System.out.println("hashSet.toString()--->"+hashSet.toString());
         * System.out.println("list.toString()--->"+list.toString());
         */
        /*
        // 數組轉爲list
        List<String> list = Arrays.asList(arr);
        System.out.println("list.toString()--->"+list.toString());
        */
        /*
        //數組轉set
        Set set = new HashSet<>(Arrays.asList(arr));
        System.out.println("set.toString()--->"+set.toString());
        */
        /*
        //map的值轉爲list
        initMap();
        List<String> list = new ArrayList<String>(hashMap.values());
        System.out.println("list.toString()--->"+list.toString());
        */
        /*
        //map的值轉爲set
        initMap();
        Set<String> set = new HashSet<String>(hashMap.values());
        System.out.println("set.toString()--->"+set.toString());
        */
        /*
        //map的key轉爲set
        initMap();
        Set<String> set = new HashSet<String>(hashMap.keySet());
        System.out.println("set.toString()--->"+set.toString());
        */
        //list轉數組
        initList();
        String[] arr1 = (String[])arrayList.toArray(new String[arrayList.size()]);
        System.out.println("Arrays.toString(arr1)--->"+Arrays.toString(arr1));
    }

    public static void initList() {
        arrayList = new ArrayList<String>();
        arrayList.add("1");
        arrayList.add("2");
        arrayList.add("3");
        arrayList.add("4");
    }

    public static void initMap() {
        hashMap = new HashMap<String, String>();
        hashMap.put("one", "one1");
        hashMap.put("two", "two2");
        hashMap.put("three", "three3");
    }

    public static void initSet() {
        hashSet = new HashSet<String>();
        hashSet.add("1one");
        hashSet.add("2two");
        hashSet.add("3three");
        hashSet.add("4four");
        hashSet.add("5five");
    }


}

我是天王蓋地虎的分割線                                                                  hash

源代碼:http://pan.baidu.com/s/1dD1Qx01

listsetmap.zip

 

 

 

轉載請註明出處:http://www.cnblogs.com/yydcdut

相關文章
相關標籤/搜索