Java集合之Map與HashMap,另含Iterator的使用

Java集合之HashMap

(一)HashMap的簡要特點

  • HashMap是最常用的Map,用於存儲鍵值對。
  • 鍵不可以重複,值可以重複。所以HashMap最多隻允許一條記錄的鍵爲Null,允許多條記錄的值爲Null。
  • HashMap是無序的,遍歷時取得數據的順序是完全隨機的。
  • HashMap是非同步的,線程不安全的。

(二)Map的遍歷

Map集合中是沒有迭代器Iterator的,而Set具備迭代器Iterator
所以Map集合取出鍵值的原理是:先將Map集合中取出的結果存儲爲Set集合,然後再通過Set集合迭代器取出。

Map的遍歷有兩種方法,一種是keySet(),一種是entrySet()。下面我們以HashMap爲栗子講解這兩種遍歷的原理與使用。

1,keySet()遍歷

keySet()用於取出Map中的所有鍵,然後Map.get(key)在Map集合中查找出這個鍵對應的值。

1)代碼栗子

    public static void keySetExample(){
        Map<String,String>  hashMap = new HashMap<String,String>();
        hashMap.put("No","006");
        hashMap.put("Age","26");
        hashMap.put("Name","碼奴");
        hashMap.put("Introduction","碼奴從來只知道前進");
        // 先keySet()取出Map集合的所有鍵並轉成Set集合
        Set<String> keys = hashMap.keySet();
        // 獲取Set集合的迭代器it
        Iterator<String> it = keys.iterator();
        // 通過迭代器遍歷Set集合的每一個元素,也就是存入的鍵key
        while(it.hasNext()){
            String key = it.next();
            //有了鍵,就可以通過map集合的get方法獲取對應的值
            String value = hashMap.get(key);
            System.out.println(key + "  :  " + value);
        }
    }

2)輸出結果

No  :  006
Introduction  :  碼奴從來只知道前進
Age  :  26
Name  :  碼奴

輸出結果與hashMap.put()的鍵順序是不一樣的,因爲HashMap.keySet()方法返回的Set數據是亂序排放的。

2,entrySet()遍歷

entrySet()用於取出Map中的所有鍵值對,然後每個鍵值對通過getKey()獲取自己的鍵,通過getValue()獲取自己的值。

hashMap.entrySet();是將hashMap裏的每一個鍵值對取出來封裝成一個Entry對象在存到一個Set裏面。
Map.Entry<String, String>的意思是一個泛型,表示Entry裏裝的是兩個String類型的字符串,分別是key和value。Map.Entry是一個接口,該接口中的getKey()和getValue()方法可以方便我們取出鍵和值。

1)代碼栗子

    public static void entrySetExample(){
        Map<String,String>  hashMap = new HashMap<String,String>();
        hashMap.put("No","005");
        hashMap.put("Age","25");
        hashMap.put("Name","碼奴");
        hashMap.put("Introduction","碼奴從來只知道前進");

        // 先entrySet()取出Map集合的所有鍵值對並存入Set集合
        Set< Map.Entry<String,String> > entrySet = hashMap.entrySet();
        // 獲取Set集合的迭代器it
        Iterator< Map.Entry<String,String> > it = entrySet.iterator();
        // 通過迭代器遍歷Set集合的每一個元素,也就是存入的鍵值對
        while(it.hasNext()){
            Map.Entry<String, String> mey = it.next();
            //getKey()和getValue是接口Map.Entry<K,V>中的方法,getKey()返回對應的鍵,getValue返回對應的值
            String key = mey.getKey();
            String value = mey.getValue();
            System.out.println(key + "  :  " + value);
        }
    }

2)輸出結果

No  :  005
Introduction  :  碼奴從來只知道前進
Age  :  25
Name  :  碼奴

(三)Map遍歷方式的抉擇

在Set集合的迭代器遍歷時,keySet()得到所有的鍵,然後hashMap.get(key)去查詢出hashMap中該鍵對應的值。

在Set集合的迭代器遍歷時,entrySet()得到所有的鍵值對,然後getKey()取出鍵,getValue()取出值。

由於hashMap.get(key)的環節,也就是得到鍵之後我們還要去查詢出hashMap中該鍵對應的值,這個查詢本身就是遍歷hashMap直到得到該鍵對應的值。
entrySet()直接就得到了所需的鍵值對,無需遍歷hashMap去獲取值,直接可以訪問自己的鍵和值。

所以推薦使用entrySet()方法,因爲少了一個遍歷hashMap直到獲取鍵對應值的環節,效率較高。

(四)更常用的寫法

1,keySet()的常用寫法

        // 先keySet()取出Map集合的所有鍵並轉成Set集合
        Set<String> keys = hashMap.keySet();
        // 獲取Set集合的迭代器it
        Iterator<String> it = keys.iterator();

寫成

        Iterator<String> it = hashMap.keySet().iterator();

2,entrySet()的常用寫法

        // 先entrySet()取出Map集合的所有鍵值對並存入Set集合
        Set< Map.Entry<String,String> > entrySet = hashMap.entrySet();
        // 獲取Set集合的迭代器it
        Iterator< Map.Entry<String,String> > it = entrySet.iterator();

寫成

        Iterator< Map.Entry<String,String> > it = hashMap.entrySet().iterator();

(五)完整代碼

package com.write;

import java.util.*;

public class RegexUtil {

    public static void keySetExample(){
        Map<String,String>  hashMap = new HashMap<String,String>();
        hashMap.put("No","006");
        hashMap.put("Age","26");
        hashMap.put("Name","碼奴");
        hashMap.put("Introduction","碼奴從來只知道前進");
        // 先keySet()取出Map集合的所有鍵並轉成Set集合
        Set<String> keys = hashMap.keySet();
        // 獲取Set集合的迭代器it
        Iterator<String> it = keys.iterator();
        // 通過迭代器遍歷Set集合的每一個元素,也就是存入的鍵key
        while(it.hasNext()){
            String key = it.next();
            //有了鍵,就可以通過map集合的get方法獲取對應的值
            String value = hashMap.get(key);
            System.out.println(key + "  :  " + value);
        }
    }

    public static void entrySetExample(){
        Map<String,String>  hashMap = new HashMap<String,String>();
        hashMap.put("No","005");
        hashMap.put("Age","25");
        hashMap.put("Name","碼奴");
        hashMap.put("Introduction","碼奴從來只知道前進");

        // 先entrySet()取出Map集合的所有鍵值對並存入Set集合
        Set< Map.Entry<String,String> > entrySet = hashMap.entrySet();
        // 獲取Set集合的迭代器it
        Iterator< Map.Entry<String,String> > it = entrySet.iterator();
        // 通過迭代器遍歷Set集合的每一個元素,也就是存入的鍵值對
        while(it.hasNext()){
            Map.Entry<String, String> mey = it.next();
            //getKey()和getValue是接口Map.Entry<K,V>中的方法,getKey()返回對應的鍵,getValue()返回對應的值
            String key = mey.getKey();
            String value = mey.getValue();
            System.out.println(key + "  :  " + value);
        }
    }

    public static void main(String[] args){
//        keySetExample();
        entrySetExample();
    }

}

 

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