JAVA基礎学習のMap集合,集合フレームワークツール類Collections,Arrays,可変パラメータ,List,Set集合フレームワークはいつ使用するかなど(4)

10028 ワード

package com.itcast.test20140113;



import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Set;



public class MainDemo {



    public static void main(String[] args) {

        // Map    

        //MapUser();



        /*

         *       ,      ,   class         ,                ?            。      ?

         * extends E   E    E      。  ,            ? super E

         *   E    E    。  ,            public void printCollection(Collection<?

         * extends Person> collection)     public void

         * printCollection(Collection<?> collection)

         */



        //          

        CollectionUtilUser();

        

        /*

         * Arrays.toString(array);//  toString[2,3,4]

         */

        List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");

        System.out.println(stooges);// [Larry, Moe, Curly]

        

        /*

         *            ,       ,                           

         *                ,                                                   

         */

        int[] array = { 1, 3, 4 };

        List<int[]> list = Arrays.asList(array);//         

        System.out.println(list);// [[I@4aa0b07b]

        Integer [] array1 = { 1, 3, 4 };

        List<Integer> integers = Arrays.asList(array1);//             

        System.out.println(integers);// [1, 3, 4]

        

        Integer [] integers2 = integers.toArray(new Integer[integers.size()]);//       

        System.out.println(Arrays.toString(integers2));

        

        //    

//        public static String Params(int... params){

//            return  Arrays.toString(params);

//        }

//        System.out.println(Params(1,2,3));//int...   int[]

        

    }

    

    



    /**

     *          

     */

    public static void CollectionUtilUser() {

        /*

         * Collections.sort(list);  

         * Collections.sort(list,CompareByStringLength);        Comparator      

         * Collections.binarySearch(list, key)//    ,    ,     ,  list     key   ,       

         * Collections.max(list);//      

         * Collections.reverse(list);//    

         * Collections.replaceAll(list, oldVal, newVal);

         * Collections.shuffle(list);//          

         * Collections.synchronizedCollection(collection);            

         * Collections.synchronizedList(list);

         * Collections.synchronizedMap(m);

         * Collections.synchronizedSet(s);

         */

    }



    /**

     * Map    

     */

    public static void MapUser() {

        /*

         * Map                : value

         * put(key,value)  value,      key    ,       key,  null void clear()

         * value remove(key) boolean containsKey(key) boolean

         * containsValue(value) boolean isEmpty() value

         * get(key)      ,      ,  null int size()

         */



        /*

         *       

         */

        Map<Integer, String> map = new HashMap<Integer, String>();

        map.put(1, "a");

        map.put(2, "b");

        map.put(3, "c");

        //      

        Set<Integer> set = map.keySet();

        // Collection<String> collection = map.values();

        for (Integer integer : set) {

            System.out.println(integer + ":" + map.get(integer));

        }

        //      

        Set<Entry<Integer, String>> entrySet = map.entrySet();

        for (Entry<Integer, String> entry : entrySet) {

            System.out.println(entry.getKey() + ":" + entry.getValue());

        }



        /*

         * Map     : Hashtable:        ,    ,   null   ,null   

         * HashMap:        ,     ,  null   ,null   。

         * TreeMap:        ,     ,   Map         

         * HashMap   LinkedHashMap:                

         */



        /*

         * List Set           

         *       :Set 

         *                       :TreeSet 

         *                                :LinkedHashSet 

         *                        : HashSet

         *                 : List 

         *                       :LinkedList 

         *                        :ArrayList

         */



    }



}