map、set認識

import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;



public class test {
    
    public static void main(String[] args) {
        Map<Long,    Long> hashMap = new HashMap<Long, Long>();
        hashMap.put(1l, 11l);
        hashMap.put(2l, 21l);
        //通過entrySet獲取值
        Iterator i = hashMap.entrySet().iterator();
        while (i.hasNext()) {
            Entry e = (Entry) i.next();
            System.out.println(e.getValue());
        }
        //通過keyset獲取值
        Iterator i1 = hashMap.keySet().iterator();
        while (i1.hasNext()) {
            Long e = (Long) i1.next();
            System.out.println(hashMap.get(e));
            
        }
        Map<Long,    Long> hashTable = new Hashtable<Long, Long>();
        
        /*********hashMap允許空,hashTable不允許空  hashTable線程安全 ***************/
        /*hashMap.put("c", null);    //不報錯
        hashMap.put(null, null);    //不報錯
        hashTable.put("a", null);    //報錯
        hashTable.put(null, null);    //報錯
        */        
        
        LinkedHashMap<Long,    Long> linkedHashMap = new LinkedHashMap<Long, Long>();
        TreeMap<Long,    Long> treeMap = new TreeMap<Long, Long>();
        System.out.println("linkedHashMap"+System.currentTimeMillis());
        for(Long j=0l ;j<1000000l;j++){
            linkedHashMap.put(j, j);
        }
        System.out.println("hashTable"+System.currentTimeMillis());
        
        for(Long j=0l ;j<1000000l;j++){
            hashTable.put(j, j);
        }
        System.out.println("hashMap"+System.currentTimeMillis());
        
        for(Long j=0l ;j<1000000l;j++){
            hashMap.put(j, j);
        }
        System.out.println("treeMap"+System.currentTimeMillis());
        for(Long j=0l ;j<1000000l;j++){
            treeMap.put(j, j);
        }
        System.out.println(System.currentTimeMillis());
        //效率方面  treeMap<linkedHashMap<hashMap<hashTable
        
        
        /*****hashSet   treeSet   linkedHashSet************/
        Set<String> hashSet = new HashSet<String>();
        hashSet.add("aaa");
        hashSet.add("bbb");
        Iterator<String> it = hashSet.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
        
        Set<String> treeSet = new TreeSet<String>();
        treeSet.add("ddd");
        treeSet.add("eee");
        
        Set<String> linkedHashSet = new LinkedHashSet<String>();
        linkedHashSet.add("fff");

        linkedHashSet.add("ggg");


        //實例化對象  反射獲取具體方法和變量
        Test test = new Test();
        System.out.println("獲取包和類"+test.getClass());
        System.out.println("獲取包和類名"+test.getClass().getName());
        System.out.println("獲取方法"+test.getClass().getMethod("methodInterface",null ));
        System.out.println("獲取方法"+test.getClass().getField("a"));
        
        //通過反射  初始化   獲取method  and  field
        Class<?> demo1=Class.forName("Test");
        try {
            demo1.newInstance();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Method[] a = demo1.getDeclaredMethods();
        for (int i = 0; i < a.length; i++) {
            System.out.println(i+"ss"+a[i]);
        }
        Field[] b = demo1.getFields();
        for (int i = 0; i < b.length; i++) {
            System.out.println(i+"ss"+b[i]);
        }
        
        Collection<String> collection = new ArrayList<String>();
        List<String> listLinked = new LinkedList<String>();
        List<String> listArray = new ArrayList<String>();
        ArrayList<String> arrays = new ArrayList<String>();
        Vector<String> listVecor = new Vector<String>();

    }
    
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章