If
you just need keys, use keySet(). If you just need values, use values(). If
you're going to use keys and values in your
subsequent code, then you're best off using entrySet().
I frequently see
people do this without entrySet(), and it
usually looks something like this:
-
for (Iterator it = map.keySet().iterator(); it.hasNext(); ) {
-
Foo key = (Foo) it.next();
-
Bar value = (Bar) map.get(key);
-
// now do something with key and value
- }
This works, but it's making the JVM do extra work for no good reason.
Every time you call get() you're making the JVM spend time doing a hashcode
lookup, or navigating a tree and evaluating a comparator. These operations may
be fast, or not, but why do them if you don't have to? A Map.Entry gives you
both key and value, together, in the most efficient manner
possible.
-
for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) {
-
Map.Entry e = (Map.Entry) it.next();
-
Foo key = (Foo) e.getKey();
-
Bar value = (Bar) e.getValue();
-
// now do something with key and value
- }
Under JDK 5 and later it's a little nicer:
-
for (Map.Entry<Foo,Bar> e : map.entrySet()) {
-
Foo key = e.getKey();
-
Bar value = e.getValue();
-
// now do something with key and value
- }
相关推荐
今天,我们来讨论一种遍历Map的方式,即使用Map.Entry和Map.entrySet()。 Map.Entry是什么? ---------------- Map.Entry是一个接口,表示一个映射项,里面有Key和Value。它有两个方法:getKey()和getValue(),...
Iterator<Map.Entry, String>> iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry, String> entry = iterator.next(); System.out.println("key=" + entry.getKey() + " and value=...
Iterator<Map.Entry, String>> it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry, String> entry = it.next(); System.out.println("key= " + entry.getKey() + " and value= " + entry....
总结,Java中遍历`Map`主要有四种方式,其中通过`Map.entrySet()`进行遍历通常被认为效率较高,尤其在`Map`容量较大的情况下。而在Java 8之后,我们可以利用Lambda表达式简化遍历操作。获取`Map`的长度直接调用`size...
1、遍历Map.entrySet():它的每一个元素都是Map.Entry对象,这个对象中, 放着的就是Map中的某一对key-value; 2、遍历Map.keySet():它是Map中key值的集合,我们可以通过遍历这个集合来 读取Map中的元素; 3、...
Map a = new HashMap(); //方法一 Iterator it = a.entrySet().iterator(); while (it.hasNext()) { ...for(Map.Entry entry:a.entrySet()){ System.out.println(entry.getKey()+"="+entry.getValue()); }
Iterator<Map.Entry, String>> it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry, String> entry = it.next(); System.out.println("key=" + entry.getKey() + " and value=" + entry....
`, `Set<Map.Entry, Integer>> entries = map.entrySet();` ArrayList是List接口的一个具体实现,它基于动态数组。ArrayList的特点是可以快速访问任意位置的元素,但插入和删除元素的效率相对较低,因为需要移动...
for (Map.Entry, Integer> entry : map.entrySet()) { System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()); } ``` 这种方法能够保证遍历顺序与插入顺序一致,但不适用于...
在上述的`HashMapTest`类中,通过对比`keySet()`和`entrySet()`遍历HashMap的时间,我们可以看到`entrySet()`方法通常比`keySet()`方法更快。`keySet()`需要两次遍历:一次是转换为迭代器,另一次是从HashMap中根据...
以上介绍了两种遍历`Map`的方法:一种是通过`entrySet()`方法,另一种是通过`keySet()`方法。其中,使用`entrySet()`可以直接获取到键值对,而使用`keySet()`则需要额外调用`get()`方法来获取对应的值。这两种方法各...
Java中提供了多种Map遍历方式,包括通过获取所有的key、通过Map.entrySet使用iterator遍历、通过Map.entrySet遍历等。 1. 通过获取所有的key遍历 ``` Set<Integer> set = map.keySet(); for (Integer in : map....
<br>for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) { <br> Map.Entry entry = (Map.Entry) iter.next(); <br> Object key = entry.getKey(); <br> Object val = entry.getValue()...
for (Map.Entry, Integer> entry : map.entrySet()) { System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); } ``` 当遍历空 Map 时,需注意避免 `NullPointerException`,所以...
System.out.println("使用 entrySet 方式显示全部键、值内容:" + map.entrySet()); ``` - **keySet()**: 返回一个Set视图,包含Map中的所有键。可以遍历这个Set来获取键值对的键: ```java for (String key : ...
Set<Map.Entry, String>> entries = map.entrySet(); // 获取所有键值对的集合 Iterator<Map.Entry, String>> iterator1 = entries.iterator(); // 获取迭代器 while (iterator1.hasNext()) { Map.Entry, String...
Iterator it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry e = (Map.Entry) it.next(); System.out.println("Key: " + e.getKey() + "; Value: " + e.getValue()); } } public static ...
Java提供了`Map`接口的`entrySet()`方法,返回一个包含`Map`中所有键值对的`Set`视图。通过这个`Set`,我们可以使用迭代器来遍历所有的键值对。 ```java Map, Integer> map = new HashMap(); for (Map.Entry, ...
for(Entry, Object> entry : map.entrySet()){ System.out.println(entry.getKey()+"::::"+entry.getValue()); } } ``` 在上面的代码中,我们首先创建了一个HashMap对象,并将其初始化为一个空的Map集合。然后,...