package com.haoran.Collection.map; import java.util.Date; import java.util.Map; import java.util.HashMap; import java.util.Hashtable; public class MapA { public static void main(String[] args) { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); Date date1= new Date(); for (int i = 0; i < 1000000; i++) { map.put(i, i); } Date date2 = new Date(); System.out.println("HashMap的插入時間:"); System.out.println(date2.getTime()-date1.getTime()); Map<Integer, Integer> map1 = new Hashtable<Integer, Integer>(); Date date3= new Date(); for (int i = 0; i < 1000000; i++) { map1.put(i, i); } Date date4 = new Date(); System.out.println("HashTable的插入時間:"); System.out.println(date4.getTime()-date3.getTime()); //write write(); //read read(); } private static void write() { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); Date date1= new Date(); for (int i = 0; i < 1000000; i++) { map.put(i, i); } Date date2 = new Date(); System.out.println("HashMap的插入時間:"); System.out.println(date2.getTime()-date1.getTime()); Map<Integer, Integer> map1 = new Hashtable<Integer, Integer>(); Date date3= new Date(); for (int i = 0; i < 1000000; i++) { map1.put(i, i); } Date date4 = new Date(); System.out.println("HashTable的插入時間:"); System.out.println(date4.getTime()-date3.getTime()); } private static void read() { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < 10000000; i++) { map.put(i, i); } Date date1= new Date(); for (Integer key : map.keySet()) { map.get(key); } Date date2 = new Date(); System.out.println("HashMap的讀取時間:"); System.out.println(date2.getTime()-date1.getTime()); Map<Integer, Integer> map1 = new Hashtable<Integer, Integer>(); for (int i = 0; i < 10000000; i++) { map1.put(i, i); } Date date3= new Date(); for (Integer key : map1.keySet()) { map1.get(key); } Date date4 = new Date(); System.out.println("HashTable的讀取時間:"); System.out.println(date4.getTime()-date3.getTime()); } }
一、Map中,HashMap具備超高的訪問速度,若是咱們只是在Map 中插入、刪除和定位元素,而無關線程安全或者同步問題,HashMap 是最好的選擇。java
二、若是考慮線程安全或者寫入速度的話,能夠使用HashTable安全
三、若是想按怎麼存的順序怎麼取,好比隊列形式,排排隊。 那麼使用LinkedHashMap吧,怎麼用怎麼爽線程
四、若是須要讓Map按照key進行升序或者降序排序,那就用TreeMap吧code