java 中的哈希映射_在Java中遍历哈希映射的内容

映射是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 map = new HashMap();

System.out.println("输入您需要存储的记录数: ");

Scanner sc = new Scanner(System.in);

int num = sc.nextInt();

for(int i=0; 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));

}

}

输出结果输入您需要存储的记录数:

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检索HashMap对象的内容,分别对每种方法和forEach()方法使用。

使用迭代器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 map = new HashMap();

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 . . . . . .");

//检索 Hash 映射的值

Iterator it = map.entrySet().iterator();

System.out.println("HashMap的内容如下: ");

while(it.hasNext()){

Map.Entry  ele = (Map.Entry) it.next();

System.out.print(ele.getKey()+" : ");

System.out.print(ele.getValue());

System.out.println();

}

}

}

输出结果Values Stored . . . . . .

HashMap的内容如下

Yousuf : 9000456789

Krishna : 9000123456

Sita : 9000345678

Rama : 9000234567

Bhima : 9000456789

示例:用于每个循环import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

public class HashMapExample {

public static void main(String args[]) {

HashMap map = new HashMap();

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 . . . . . .");

//检索 Hash 映射的值

Iterator it = map.entrySet().iterator();

System.out.println("HashMap的内容如下: ");

for(Map.Entry ele : map.entrySet()){

System.out.print(ele.getKey()+" : ");

System.out.print(ele.getValue());

System.out.println();

}

}

}

输出结果Values Stored . . . . . .

HashMap的内容如下:

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 map = new HashMap();

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("HashMap的内容如下: ");

map.forEach((k, v) -> System.out.println(k +": " + (v)));

}

}

输出结果Values Stored . . . . . .

HashMap的内容如下:

Yousuf: 9000456789

Krishna: 9000123456

Sita: 9000345678

Rama: 9000234567

Bhima: 9000456789

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值