Difference Between Map and MultivaluedMap in Java – Java中Map和MultivaluedMap的区别

最后修改: 2022年 11月 5日

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

1. Overview

1.概述

In this tutorial, we’ll learn the difference between Map and MultivaluedMap in Java. But before that, let’s take a look at some examples.

在本教程中,我们将学习Java中MapMultivaluedMap之间的区别。但在这之前,让我们先看看一些例子。

2. Example for Map

2.例子:地图

HashMap implements the Map interface, and it also permits null values and null keys:

HashMap实现了Map接口,它也允许null值和null键。

@Test
public void givenHashMap_whenEquals_thenTrue() {
    Map<String, Integer> map = new HashMap<>();

    // Putting key-value pairs into our map.
    map.put("first", 1);
    map.put(null, 2);
    map.put("third", null);

    // The assert statements. The last arguments is what's printed if the assertion fails.
    assertNotNull(map, "The HashMap is null!");
    assertEquals(1, map.get("first"), "The key isn't mapped to the right value!");
    assertEquals(2, map.get(null), "HashMap didn't accept null as key!");
    assertEquals(null, map.get("third"), "HashMap didn't accept null value!");
}

The above unit test passes successfully. As we can see, each key is mapped to exactly one value.

上述单元测试成功通过。正如我们所看到的,每个键都被精确地映射到一个值。

3. How to Add MultivaluedMap to Our Project?

3.如何在我们的项目中添加MultivaluedMap

Before using the MultivaluedMap interface and its implementing classes, we need to add its library, Jakarta RESTful WS API, to our Maven project. To do that, we need to declare a dependency in the project’s pom.xml file:

在使用MultivaluedMap接口及其实现类之前,我们需要将其库Jakarta RESTful WS API加入我们的Maven项目。为此,我们需要在项目的pom.xml文件中声明一个依赖项。

<dependency>
    <groupId>jakarta.ws.rs</groupId>
    <artifactId>jakarta.ws.rs-api</artifactId>
    <version>3.1.0</version>
</dependency>

We’ve successfully added the library to our project.

我们已经成功地将该库添加到我们的项目中。

4. Example for MultivaluedMap

4.多值图的例子

MultivaluedHashMap implements the MultivaluedMap interface, and it permits null values and null keys:

MultivaluedHashMap实现了MultivaluedMap接口,它允许null值和null键。

@Test
public void givenMultivaluedHashMap_whenEquals_thenTrue() {
    MultivaluedMap<String, Integer> mulmap = new MultivaluedHashMap<>();

    // Mapping keys to values.
    mulmap.addAll("first", 1, 2, 3);
    mulmap.add(null, null);

    // The assert statements. The last argument is what's printed if the assertion fails.
    assertNotNull(mulmap, "The MultivaluedHashMap is null!");
    assertEquals(1, mulmap.getFirst("first"), "The key isn't mapped to the right values!");
    assertEquals(null, mulmap.getFirst(null), "MultivaluedHashMap didn't accept null!");
}

The above unit test passes successfully. Here, each key is mapped to a list of values that can contain zero or more elements.

上述单元测试成功通过。在这里,每个键都被映射到一个可以包含零个或多个元素的值的列表。

5. What are the Differences?

5.差异是什么?

In the Java ecosystem, Map and MultivaluedMap are both interfaces. The difference is that, in a Map, every key is mapped to exactly one object. However, in a MultivaluedMap, we can have zero or more objects associated with the same key.

在Java生态系统中,MapMultivaluedMap都是接口。区别在于,在Map中,每个键都被精确地映射到一个对象。然而,在MultivaluedMap中,我们可以有零个或多个对象与同一个键相关联。

Further, MultivaluedMap is a subinterface of Map, so it has all of its methods and a few more of its own. Some of the classes that implement Map are HashMap, LinkedHashMap, ConcurrentHashMap, WeakHashMap, EnumMap, and TreeMap. Moreover, MultivaluedHashMap is a class that implements both Map and MultivaluedMap.

此外,MultivaluedMapMap的一个子接口,所以它拥有它的所有方法和一些自己的方法。实现Map的一些类是HashMap, LinkedHashMap, ConcurrentHashMap, WeakHashMap, EnumMap, 和TreeMap。此外,MultivaluedHashMap是一个同时实现了MapMultivaluedMap的类。

For instance, the addFirst(K key, V value) is one of MultivaluedMap‘s methods that adds a value to the first position in the current list of values for the supplied key:

例如,addFirst(K key, V value)MultivaluedMap的方法之一,它为所提供的键的当前值列表中的第一个位置添加一个值。

MultivaluedMap<String, String> mulmap = new MultivaluedHashMap<>();
mulmap.addFirst("firstKey", "firstValue");

On the other hand, getFirst(K key) gets the first value of the supplied key:

另一方面,getFirst(K key)获得所提供的键的第一个值。

String value = mulmap.getFirst("firstKey");

And finally, addAll(K key, V… newValues) adds multiple values to the current list of values for the supplied key:

最后,addAll(K key, V… newValues)将多个值添加到提供的键的当前值列表中。

mulmap.addAll("firstKey", "secondValue", "thirdValue");

6. Summary

6.归纳总结

In this article, we saw the differences between Map and MultivaluedMap.

在这篇文章中,我们看到了MapMultivaluedMap的区别。

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

一如既往,完整的源代码可在GitHub上获得