Java 中遍歷雜湊對映內容


對映是 Java 中的一種集合,它儲存鍵值對。這些鍵不能為 null,並且每個鍵都應該只指向一個值。它由 java.util 包的 Map 介面表示。有各種類為該介面提供實現。

HashMap 是一個實現 Map 介面的類。它基於雜湊表。它允許 null 值和 null 鍵。

簡而言之,您可以在 HashMap 物件中儲存鍵值對。這樣做後,您可以檢索相應鍵的值,但是,我們用於鍵的值應該是唯一的。

示例

 線上演示

import java.util.HashMap;
import java.util.Scanner;
public class HashMapExample {
   public static void main(String args[]) {
      HashMap<String, Long> map = new HashMap<String, Long>();
      System.out.println("Enter the number of records you need to store: ");
      Scanner sc = new Scanner(System.in);
      int num = sc.nextInt();
      for(int i=0; i<num; i++) {
         System.out.println("Enter key (String): ");
         String key = sc.next();
         System.out.println("Enter value (Long): ");
         long value = sc.nextLong();
         map.put(key, value);
      }
      System.out.println("Values Stored . . . . . .");
      System.out.println("Enter a name (key): ");
      String reqKey = sc.next();
      System.out.println("Phone number (value): "+map.get(reqKey));
   }
}

輸出

Enter the number of records you need to store:
3
Enter key (String):
Krishna
Enter value (Long):
9848022337
Enter key (String):
Vishnu
Enter value (Long):
9848022338
Enter key (String):
Moksha
Enter value (Long):
9848022339
Values Stored . . . . . .
Enter a name (key):
Krishna
Phone number (value): 9848022337

檢索 HashMap 的內容

您可以使用迭代器、使用 for each 方法以及使用 forEach() 方法來檢索 HashMap 物件的內容。

使用迭代器

  • HashMap 類的 entrySet() 返回當前(HashMap)物件的集合檢視。

  • 在獲得的 Set 物件上呼叫 iterator() 方法。此方法返回當前 HashMap 的 Iterator 物件。

  • 迭代器類的 hasNext() 方法如果迭代器包含更多元素則返回 true,其 next() 方法返回下一個對映條目。

  • Map.Entry 的 getKey()getValue() 方法分別返回鍵和值。

示例

 線上演示

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class HashMapExample {
   public static void main(String args[]) {
      HashMap<String, Long> map = new HashMap<String, Long>();
      map.put("Krishna", 9000123456L);
      map.put("Rama", 9000234567L);
      map.put("Sita", 9000345678L);
      map.put("Bhima", 9000456789L);
      map.put("Yousuf ", 9000456789L);
      System.out.println("Values Stored . . . . . .");
      //Retrieving the values of a Hash map
      Iterator it = map.entrySet().iterator();
      System.out.println("Contents of the hashMap are: ");
      while(it.hasNext()){
         Map.Entry <String, Long> ele = (Map.Entry) it.next();
         System.out.print(ele.getKey()+" : ");
         System.out.print(ele.getValue());
         System.out.println();
      }
   }
}

輸出

Values Stored . . . . . .
Contents of the hashMap are:
Yousuf : 9000456789
Krishna : 9000123456
Sita : 9000345678
Rama : 9000234567
Bhima : 9000456789

示例:使用 for each 迴圈

 線上演示

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class HashMapExample {
   public static void main(String args[]) {
      HashMap<String, Long> map = new HashMap<String, Long>();
      map.put("Krishna", 9000123456L);
      map.put("Rama", 9000234567L);
      map.put("Sita", 9000345678L);
      map.put("Bhima", 9000456789L);
      map.put("Yousuf ", 9000456789L);
      System.out.println("Values Stored . . . . . .");
      //Retrieving the values of a Hash map
      Iterator it = map.entrySet().iterator();
      System.out.println("Contents of the hashMap are: ");
      for(Map.Entry ele : map.entrySet()){
         System.out.print(ele.getKey()+" : ");
         System.out.print(ele.getValue());
         System.out.println();
      }
   }
}

輸出

Values Stored . . . . . .
Contents of the hashMap are:
Yousuf : 9000456789
Krishna : 9000123456
Sita : 9000345678
Rama : 9000234567
Bhima : 9000456789

使用 forEach() 方法

從 Java8 開始,引入了一個名為 forEach() 的方法來檢索 HashMap 的內容。這是從 HashMap 中檢索元素的最簡單方法。

示例

 線上演示

import java.util.HashMap;
public class HashMapExample {
   public static void main(String args[]) {
      HashMap<String, Long> map = new HashMap<String, Long>();
      map.put("Krishna", 9000123456L);
      map.put("Rama", 9000234567L);
      map.put("Sita", 9000345678L);
      map.put("Bhima", 9000456789L);
      map.put("Yousuf ", 9000456789L);
      System.out.println("Values Stored . . . . . .");
      //Retrieving the values of a Hash map
      System.out.println("Contents of the hashMap are: ");
      map.forEach((k, v) -> System.out.println(k +": " + (v)));
   }
}

輸出

Values Stored . . . . . .
Contents of the hashMap are:
Yousuf: 9000456789
Krishna: 9000123456
Sita: 9000345678
Rama: 9000234567
Bhima: 9000456789

更新於: 2019-09-06

635 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.