Get the First Key and Value From a HashMap – 从HashMap中获取第一个键和值

最后修改: 2020年 9月 24日

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

1. Overview

1.概述

In this tutorial, we’ll discuss how to get the first key-value pair from a HashMap without knowing the key.

在本教程中,我们将讨论如何在不知道键的情况下从HashMap中获得第一个键-值对。

First, we’ll use an iterator, and then a stream to get the first entry. Finally, we’ll discuss a problem the HashMap presents when we want to get the first entry and how to solve it.

首先,我们将使用一个迭代器,然后使用流来获取第一个条目。最后,我们将讨论HashMap在我们想要获得第一个条目时出现的问题以及如何解决它。

2. Using an Iterator

2.使用迭代器

Let’s consider we have the following HashMap<Integer, String>:

让我们考虑我们有以下HashMap<Integer, String>

Map<Integer, String> hashMap = new HashMap<>();
hashMap.put(5, "A");
hashMap.put(1, "B");
hashMap.put(2, "C");

In this example, we’ll use an iterator to get the first key-value pair. So, let’s create an iterator on the entry set of the HashMap and call the next() method to retrieve the first entry:

在这个例子中,我们将使用一个iterator来获取第一个键值对。因此,让我们在HashMap条目集上创建一个iterator,并调用next()方法来检索第一个条目。

Iterator<Map.Entry<Integer, String>> iterator = hashMap.entrySet().iterator();

Map.Entry<Integer, String> actualValue = iterator.next();
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(1, "B");

assertEquals(expectedValue, actualValue);

3. Using a Java Stream

3.使用一个Java流

Another approach is to use the Java Stream API. Let’s create a stream on the entry set and call the findFirst() method to get its first entry:

另一种方法是使用Java Stream API。让我们在条目集上创建一个流,并调用findFirst() 方法来获取其第一个条目。

Map.Entry<Integer, String> actualValue = hashMap.entrySet()
  .stream()
  .findFirst()
  .get();
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(1, "B");

assertEquals(expectedValue, actualValue);

4. Problem With the Insertion Order

4.插入顺序的问题

To present this problem, let’s remember how we created hashMap, the pair 5=A was inserted as the first entry, then  1=B and finally 2=C. Let’s check this by printing the content of our HashMap:

为了说明这个问题,让我们回忆一下我们是如何创建hashMap的,一对5=A被插入作为第一个条目,然后是1=B,最后是2=C。让我们通过打印我们的HashMap的内容来检查这一点。

System.out.println(hashMap);
{1=B, 2=C, 5=A}

As we can see, ordering is not the same. The HashMap class implementation doesn’t guarantee the insertion order.

我们可以看到,排序是不一样的。HashMap类的实现并不保证插入顺序

Let’s now add one more element to hashMap:

现在让我们为hashMap增加一个元素。

hashMap.put(0, "D");

Iterator<Map.Entry<Integer, String>> iterator = hashMap.entrySet().iterator();

Map.Entry<Integer, String> actualValue = iterator.next();
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(0, "D");

assertEquals(expectedValue, actualValue);

As we can see, the first entry has changed again (to 0=D in this case). This also proves that HashMap doesn’t guarantee an insertion order.

我们可以看到,第一个条目又发生了变化(在这种情况下为0=D)。这也证明了HashMap并不能保证插入的顺序。

So, if we want to preserve the order, we should use a LinkedHashMap instead:

所以,如果我们想保留顺序,我们应该使用LinkedHashMap代替

Map<Integer, String> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put(5, "A");
linkedHashMap.put(1, "B");
linkedHashMap.put(2, "C");
linkedHashMap.put(0, "D");

Iterator<Map.Entry<Integer, String>> iterator = linkedHashMap.entrySet().iterator();
Map.Entry<Integer, String> actualValue = iterator.next();
Map.Entry<Integer, String> expectedValue = new AbstractMap.SimpleEntry<Integer, String>(5, "A");

assertEquals(expectedValue, actualValue);

5. Conclusion

5.总结

In this short article, we discussed different approaches to get the first entry from a HashMap.

在这篇短文中,我们讨论了从HashMap中获取第一个条目的不同方法。

The most important point to note is that HashMap implementation doesn’t guarantee any order of insertion. So, if we’re interested in preserving the insertion order, we should use a LinkedHashMap.

最重要的一点是,HashMap的实现并不保证任何插入的顺序。因此,如果我们对保留插入顺序感兴趣,我们应该使用LinkedHashMap

The code example is available over on GitHub.

该代码示例可在GitHub上获得