列舉幾個關於Java Map的常見問題並給出答案。java
Map接口提供了三種collection:key set,value set 和 key-value set,每一種均可以轉成List。以下:面試
//mapHashMap<Integer,Integer> map = new HashMap<>();map.put(1,10);map.put(2,20);map.put(3,30);//key listArrayList<Integer> keyList = new ArrayList<>(map.keySet());//value listArrayList<Integer> valueList = new ArrayList<>(map.values());//key-value listArrayList<Map.Entry<Integer,Integer>> entryList = new ArrayList<>(map.entrySet());
最高效的遍歷map的每一個entry的方法以下:數組
for (Map.Entry entry : map.entrySet()){ int key = (int) entry.getKey(); int value = (int) entry.getValue();}
也可使用iterator,特別是JDK 1.5以前。安全
Iterator itr = map.entrySet().iterator();while(itr.hasNext()){ Map.Entry entry = itr.next(); int key = (int) entry.getKey(); int value = (int) entry.getValue();}
能夠將Map.Entry放入一個list,而後本身實現Comparator來對list排序。架構
ArrayList<Map.Entry<Integer,Integer>> list = new ArrayList<>(map.entrySet());Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() { @Override public int compare(Map.Entry<Integer, Integer> e1, Map.Entry<Integer, Integer> e2) { return e1.getKey().compareTo(e2.getKey()); }});
可使用SortedMap。SortedMap的一個實現類是TreeMap。TreeMap的構造器能夠接受一個Comparator參數。以下:框架
SortedMap<Integer,Integer> sortedMap = new TreeMap<>(new Comparator<Integer>() { @Override public int compare(Integer k1, Integer k2) { return k1.compareTo(k2); }});sortedMap.putAll(map);
注:TreeMap默認對key進行排序。分佈式
ArrayList<Map.Entry<Integer,Integer>> list = new ArrayList<>(map.entrySet());Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() { @Override public int compare(Map.Entry<Integer, Integer> e1, Map.Entry<Integer, Integer> e2) { return e1.getValue().compareTo(e2.getValue()); }});
若是map中的value不重複,能夠經過反轉key-value對爲value-key對來用上面的3中的TreeMap方法對其排序。該方法不推薦。ide
正確的作法:函數
public class Test{ private static Map<Integer,Integer> map1 = new HashMap<>(); static { map1.put(8,9); map1.put(88,99); map1 = Collections.unmodifiableMap(map1); }}
錯誤的作法:源碼分析
public class Test{ private static final Map<Integer,Integer> map1 = new HashMap<>(); static { map1.put(8,9); map1.put(88,99); }}
加了final只能確保不能 map1 = new,可是能夠修改map1中的元素。
Map接口有三個比較重要的實現類,分別是HashMap、TreeMap和HashTable。
TreeMap是有序的,HashMap和HashTable是無序的。
Hashtable的方法是同步的,HashMap的方法不是同步的。這是二者最主要的區別。
這就意味着Hashtable是線程安全的,HashMap不是線程安全的。HashMap效率較高,Hashtable效率較低。 若是對同步性或與遺留代碼的兼容性沒有任何要求,建議使用HashMap。 查看Hashtable的源代碼就能夠發現,除構造函數外,Hashtable的全部 public 方法聲明中都有 synchronized關鍵字,而HashMap的源碼中則沒有。
Hashtable不容許null值,HashMap容許null值(key和value都容許)
父類不一樣:Hashtable的父類是Dictionary,HashMap的父類是AbstractMap
Hashtable中hash數組默認大小是11,增長的方式是 old*2+1。HashMap中hash數組的默認大小是16,並且必定是2的指數。
| HashMap | Hashtable | TreeMap ------------------------------------------------------- iteration order | no | no | yes null key-value | yes-yes | no-no | no-yes synchronized | no | yes | no time performance | O(1) | O(1) | O(log n) implementation | buckets | buckets | red-black tree
若是但願該map爲不可變的,則:
map = Collections.emptyMap();
不然:
map = new HashMap();
若是你想學習Java工程化、高性能及分佈式、高性能、深刻淺出。性能調優、Spring,MyBatis,Netty源碼分析和大數據等知識點能夠來找我。 而如今我就有一個平臺能夠提供給大家學習,讓你在實踐中積累經驗掌握原理。主要方向是JAVA架構師。若是你想拿高薪,想突破瓶頸,想跟別人競爭能取得優點的,想進BAT可是有擔憂面試不過的,能夠加個人Java架構進階羣:554355695 注:加羣要求 一、具備2-5工做經驗的,面對目前流行的技術不知從何下手,須要突破技術瓶頸的能夠加。 二、在公司待久了,過得很安逸,但跳槽時面試碰壁。須要在短期內進修、跳槽拿高薪的能夠加。 三、若是沒有工做經驗,但基礎很是紮實,對java工做機制,經常使用設計思想,經常使用java開發框架掌握熟練的,能夠加。 四、以爲本身很牛B,通常需求都能搞定。可是所學的知識點沒有系統化,很難在技術領域繼續突破的能夠加。 5.阿里Java高級大牛直播講解知識點,分享知識,多年工做經驗的梳理和總結,帶着你們全面、科學地創建本身的技術體系和技術認知! 6.小號加羣一概不給過,謝謝。