Java Map – keySet() vs. entrySet() vs. values() Methods – Java Map – keySet() vs. entrySet() vs. values() 方法

最后修改: 2021年 10月 25日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll discuss the three methods keySet(), entrySet() and values() of the Map interface in Java. These methods are used to retrieve a set of keys, a set of key-value mappings, and a collection of values, respectively.

在本教程中,我们将讨论Java中Map接口的三个方法keySet()、entrySet()和values() 。这些方法分别用于检索一组键、一组键值映射和一组值的集合。

2. Map Initialization

2.地图初始化

While we can use these methods on any class implementing the Map interface like HashMap, TreeMap, and LinkedHashMap, we’ll work with HashMap here.

虽然我们可以在任何实现Map接口的类上使用这些方法,比如HashMap, TreeMap, 和 LinkedHashMap,我们将在这里使用HashMap。

Let’s create and initialize a HashMap whose key is of type String and value is of type Integer:

让我们创建并初始化一个HashMap,它的键是String类型,值是Integer类型。

Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);

3. The keySet() Method

3.keySet()方法

The keySet() method returns the Set of keys contained in the Map. 

keySet()方法返回Map中包含的键的Set。

Let’s apply the method keySet() to the Map and store it in a Set variable actualValues:

让我们将方法keySet()应用于Map,并将其存储在Set变量actualValues中。

Set<String> actualValues = map.keySet();

Now, let’s confirm that the size of the returned Set is 2:

现在,让我们确认返回的Set的大小是2。

assertEquals(2, actualValues.size());

Further, we can see that the returned Set contains the keys of the Map:

此外,我们可以看到,返回的Set包含Map的键。

assertTrue(actualValues.contains("one"));
assertTrue(actualValues.contains("two"));

4. The entrySet() Method

4.entrySet()方法

The entrySet() method returns the set of key-value mappings. The method doesn’t take any parameters and has a return type Set of Map.Entry. 

entrySet()方法返回键值映射的集合。该方法不接受任何参数,其返回类型为Set的Map.Entry。

Let’s apply the method entrySet() to the Map:

让我们将方法entrySet()应用于Map:。

Set<Map.Entry<String, Integer>> actualValues = map.entrySet();

As we can see, actualValues is a Set of Map.Entry objects.

我们可以看到,actualValues是一个Set的Map.Entryobjects。

Map.Entry is a static interface that holds both the key and the value. Internally, it has two implementations – AbstractMap.SimpleEntry and AbstractMap.SimpleImmutableEntry.

Map.Entry是一个静态接口,它同时持有键和值。在内部,它有两个实现 – AbstractMap.SimpleEntry和AbstractMap.SimpleImmutableEntry。

As before, let’s confirm that the size of the returned Set is 2:

像以前一样,让我们确认返回的Set的大小是2。

assertEquals(2, actualValues.size());

Furthermore, we can see that the returned Set contains the key-value entries of the Map:

此外,我们可以看到,返回的Set包含Map的键值条目。

assertTrue(actualValues.contains(new SimpleEntry<>("one", 1)));
assertTrue(actualValues.contains(new SimpleEntry<>("two", 2)));

Here, we’ve chosen the AbstractMap.SimpleEntry implementation of the interface Map.Entry for our test.

在这里,我们选择了AbstractMap.SimpleEntry接口的实现Map.Entry来进行测试。

5. The values() Method

5.values()方法

The values() method returns the Collection of values contained in the Map. The method doesn’t take any parameters and has a return type Collection. 

values()方法返回Map中包含的值的Collection。该方法不接受任何参数,并且有一个返回类型Collection。

Let’s apply the method values() to the Map and store it in a Collection variable actualValues:

让我们将方法values()应用于Map,并将其存储在CollectionvariableactualValues:。

Collection<Integer> actualValues = map.values();

Now, let’s verify the size of the returned Collection:

现在,让我们验证一下返回的Collection的大小:。

assertEquals(2, actualValues.size());

Further, we can see that the returned Set contains the values of the Map:

此外,我们可以看到,返回的Set包含Map的值。

assertTrue(actualValues.contains(1));
assertTrue(actualValues.contains(2));

6. Conclusion

6.结论

In this article, we’ve discussed the keySet(), entrySet(), and values() methods of the Map interface.

在本文中,我们讨论了Map接口的keySet()、entrySet()和values()方法。

As usual, the complete source code is available over on GitHub.

像往常一样,完整的源代码可以在GitHub上找到,。