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
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP