在 Java 中遍历哈希映射的内容
java 8object oriented programmingprogramming更新于 2025/11/7 10:20:17
映射是 Java 中存储键值对的集合。映射的键不能为空,并且每个键只能指向一个值。它由 java.util 包中的 Map 接口表示。有多个类实现了此接口。
HashMap 是实现 Map 接口的类。它基于哈希表。它允许值为空,键也为空。
简而言之,您可以在 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 的内容
您可以使用迭代器 (Iterator)、foreach 方法和 forEach() 方法检索 HashMap 对象的内容。
使用迭代器 (Iterator)
HashMap 类的 entrySet() 方法返回当前 (HashMap) 对象的集合视图。
在获取的 Set 对象上调用 iterator() 方法。此方法返回当前 HashMap 的 Iterator 对象。
如果迭代器包含更多元素,迭代器类的 hasNext() 方法返回 true,其 next() 方法返回下一个 Map 条目。
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 . . . . . .");
//检索哈希映射的值
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 . . . . . .");
//检索哈希映射的值
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() 方法
自 Java 8 起,引入了一个名为 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 . . . . . .");
//检索哈希映射的值
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

