如何在 Java 中迭代 Map
在 Java 中,可以通过多种方式对 Map 进行迭代。 请记住,我们不能直接使用迭代器对 map 进行迭代,因为 Map 接口不是 Collection 的一部分。 Java 中的所有map都实现了 Map 接口。 Java中有以下类型的map:
Map 不是集合,但仍然可以在集合框架使用。 因此, Map 是一个不继承 Collections 接口的接口。
迭代器 Iterator
迭代器是用于迭代集合的接口。 它取代了 Java 集合框架中的枚举。 迭代器和枚举的区别在于:
- Iterator 可以遍历遗留和非遗留元素,而 Enumeration 只能遍历遗留元素。
- 迭代器是 fail-fast 的,而枚举不是。
集合视图
集合 views 方法允许通过以下方式将map作为集合使用:
- keySet :是 Map 中包含的键的集合。
- values :它是 Map 中包含的值的集合。
- entrySet :Map 中的一组键值对。
Map 接口还有一个称为 Map.entry 的小型嵌套接口。 集合 views 是迭代 map 的唯一方法。
使用 Iterator 接口
public static void main(String args[])
{
HashMap<Integer, String> hm = new HashMap<Integer, String>(); // 实现 map 接口
hm.put(110,"Ravi");
hm.put(120,"Prateek");
hm.put(130, "Davesh");
hm.put(140, "Kamal");
hm.put(150, "Pawan");
Iterator <Integer> it = hm.keySet().iterator(); //keyset 是一个方法
while(it.hasNext())
{
int key=(int)it.next();
System.out.println("Roll no.: "+key+" name: "+hm.get(key));
}
}
上面程序编译运行结果如下
Roll no.: 130 name: Davesh
Roll no.: 150 name: Pawan
Roll no.: 120 name: Prateek
Roll no.: 140 name: Kamal
Roll no.: 110 name: Ravi
使用 keyset() 和 value() 方法迭代Map
keyset() :HashMap 类的 keySet() 方法用于对map中包含的键进行迭代。 它返回键的集合视图。
下面是它的语法
Set<K> keyset()
values() :HashMap 类的 values() 方法用于对map中包含的值进行迭代。 它返回值的集合视图。
下面是它的语法
Collection<V> values()
示例
import java.util.Map;
import java.util.HashMap;
class IterationExample2
{
public static void main(String[] arg)
{
Map<String,String> map = new HashMap<String,String>();
map.put("Gujarat", "Gandhi Nagar");
map.put("Uttar Pradesh", "Lucknow");
map.put("Sikkim", "Ganagtok");
for (String State : map.keySet()) // 使用 keyset() 方法对 keySet 进行迭代
System.out.println("State: " + State);
for (String Capital : map.values()) // 使用 values() 对键进行迭代
System.out.println("Capiatl: " + Capital);
}
}
上面示例运行结果如下
State: Gujarat
State: Sikkim
State: Uttar Pradesh
Capiatl: Gandhi Nagar
Capiatl: Ganagtok
Capiatl: Lucknow
使用 Map.entry<K,V> 方法
Map.Entry<K,V> 是一个接口。 它返回map的集合视图,其元素属于此类。
map.entrySet() 方法返回map中包含的映射的 Set 视图。 Map中的变化也会反映在 Set 中,反之亦然。 它还支持元素移除,即从Map中移除相应的映射。
它的语法如下
Set<Map.Entry<K,V>> entrySet()
示例
public class IterationExample3
{
public static void main(String[] arg)
{
Map<String, Float> map = new HashMap<String, Float>();
map.put("Cookies", 90.87f);
map.put("Dry Fruits", 434.23f);
map.put("Oats", 220.00f);
map.put("Chocolate", 70.89f);
for (Map.Entry<String,Float> entry : map.entrySet()) //使用 map.entrySet() 遍历
{
// 返回响应的 key 和 value
System.out.println("Item: " + entry.getKey() + ", Price: " + entry.getValue());
}
}
}
运行结果如下
Item: Oats, Price: 220.0
Item: Dry Fruits, Price: 434.23
Item: Cookies, Price: 90.87
Item: Chocolate, Price: 70.89
迭代 key 并获取 value
在下面的示例中,我们首先遍历 key,然后获取value。
import java.util.*;
public class MapStudy {
public static void main(String args[])
{
HashMap<Integer, String> hm = new HashMap<Integer, String>(); // 实现 map 接口
hm.put(110,"Ravi");
hm.put(120,"Prateek");
hm.put(130, "Davesh");
hm.put(140, "Kamal");
hm.put(150, "Pawan");
for (Integer Key: hm.keySet()) //迭代遍历key
{
// 返回指定 key 映射到的值
String lastname=hm.get(Key);
System.out.println("Key: " + Key + ", Value: " + lastname);
}
}
}
上面示例运行结果如下
Key: 130, Value: Davesh
Key: 150, Value: Pawan
Key: 120, Value: Prateek
Key: 140, Value: Kamal
Key: 110, Value: Ravi
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
如何在 JavaScript 中合并两个数组而不出现重复的情况
发布时间:2024/03/23 浏览次数:86 分类:JavaScript
-
本教程介绍了如何在 JavaScript 中合并两个数组,以及如何删除任何重复的数组。