Java Map中的幾個常見問題

列舉幾個關於Java Map的常見問題並給出答案。

1. 將Map轉化成List

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());

2. 迭代Map

最高效的遍歷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();}

3. 根據key對map進行排序

可以將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進行排序。

4. 根據value對map進行排序

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方法對其排序。該方法不推薦。

5. 初始化一個不可變Map

正確的做法:

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中的元素。

6. HashMap、TreeMap和HashTable的區別

Map接口有三個比較重要的實現類,分別是HashMap、TreeMap和HashTable。

  1. TreeMap是有序的,HashMap和HashTable是無序的。

  2. Hashtable的方法是同步的,HashMap的方法不是同步的。這是兩者最主要的區別。

    這就意味着Hashtable是線程安全的,HashMap不是線程安全的。HashMap效率較高,Hashtable效率較低。 如果對同步性或與遺留代碼的兼容性沒有任何要求,建議使用HashMap。 查看Hashtable的源代碼就可以發現,除構造函數外,Hashtable的所有 public 方法聲明中都有 synchronized關鍵字,而HashMap的源碼中則沒有。

  3. Hashtable不允許null值,HashMap允許null值(key和value都允許)

  4. 父類不同:Hashtable的父類是Dictionary,HashMap的父類是AbstractMap

  5. 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

7. 創建一個空的Map

如果希望該map爲不可變的,則:

map = Collections.emptyMap();

否則:

map = new HashMap();

如果你想學習Java工程化、高性能及分佈式、高性能、深入淺出。性能調優、Spring,MyBatis,Netty源碼分析和大數據等知識點可以來找我。


而現在我就有一個平臺可以提供給你們學習,讓你在實踐中積累經驗掌握原理。主要方向是JAVA架構師。如果你想拿高薪,想突破瓶頸,想跟別人競爭能取得優勢的,想進BAT但是有擔心面試不過的,可以加我的Java架構進階羣:554355695


注:加羣要求


1、具有2-5工作經驗的,面對目前流行的技術不知從何下手,需要突破技術瓶頸的可以加。 
2、在公司待久了,過得很安逸,但跳槽時面試碰壁。需要在短時間內進修、跳槽拿高薪的可以加。 
3、如果沒有工作經驗,但基礎非常紮實,對java工作機制,常用設計思想,常用java開發框架掌握熟練的,可以加。 
4、覺得自己很牛B,一般需求都能搞定。但是所學的知識點沒有系統化,很難在技術領域繼續突破的可以加。 
5.阿里Java高級大牛直播講解知識點,分享知識,多年工作經驗的梳理和總結,帶着大家全面、科學地建立自己的技術體系和技術認知! 
6.小號加羣一律不給過,謝謝。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章