Apache Commons Collections BidiMap – 阿帕奇共享资源收集 BidiMap

最后修改: 2017年 7月 9日

1. Overview

1.概述

In this short article, we’ll be looking at an interesting data structure in the Apache Commons Collections library – the BidiMap.

在这篇短文中,我们将研究Apache Commons集合库中的一个有趣的数据结构–BidiMap

The BidiMap adds a possibility of looking up the key using the corresponding value on top of the standard Map interface.

BidiMap在标准Map接口的基础上,增加了使用相应的值来查找键的可能性。

2. Dependencies

2.依赖性

We need to include the following dependency in our project for us to use BidiMap and its implementations. For Maven based projects, we have to add the following dependency to our pom.xml:

为了使用BidiMap及其实现,我们需要在我们的项目中加入以下依赖。对于基于Maven的项目,我们必须在pom.xml中添加以下依赖项。

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.1</version>
</dependency>

For Gradle-based projects, we have to add the same artifact to our build.gradle file:

对于基于Gradle的项目,我们必须在我们的build.gradle文件中添加同样的神器。

compile 'org.apache.commons:commons-collections4:4.1'

The latest version of this dependency can be found on Maven Central.

该依赖的最新版本可以在Maven中心找到

3. Implementations and Instantiation

3.实施和实例化

BidiMap itself is just an interface that defines behaviors unique to a bi-directional map – and there are of course multiple implementations available.

BidiMap本身只是一个接口,它定义了双向地图所特有的行为–当然也有多种实现可用。

It’s important to understand that implementations of BidiMap do not allow key and value duplicates. When a BidiMap gets inverted, any duplicate values will be converted to duplicate keys and will violate the map contract. A map must always have unique keys.

重要的是要理解,BidiMap的实现不允许键和值的重复。当一个BidiMap被倒置时,任何重复的值都会被转换为重复的键,并且会违反地图契约。一个地图必须总是有唯一的键。

Let’s look at different concrete implementations of this interface:

我们来看看这个接口的不同具体实现。

  • DualHashBidiMap: This implementation uses two HashMap instances to implement the BidiMap internally. It provides fast lookup of entries using either an entry’s key or value. However, two instances of HashMap have to be maintained
  • DualLinkedHashBidiMap: This implementation uses two LinkedHashMap instances and consequently maintains the insertion order of map entries. If we don’t need the insert order of the map entries to be maintained, we can just use the less expensive DualHashBidiMap
  • TreeBidiMap: This implementation is efficient and is realized by a Red-Black tree implementation. The keys and values of TreeBidiMap are guaranteed to be sorted in ascending order using the natural ordering of the keys and values
  • There is also DualTreeBidiMap that uses two instances of TreeMap to achieve the same thing as TreeBidiMap. DualTreeBidiMap is obviously more expensive than TreeBidiMap

The BidiMap interface extends the java.util.Map interface and so can serve as a drop-in replacement for it. We can use the no-arg constructor of the concrete implementations to instantiate a concrete object instance.

BidiMap接口扩展了java.util.Map接口,因此可以作为它的直接替换。我们可以使用具体实现的无条件构造函数来实例化一个具体的对象实例.

4. Unique BidiMap Methods

4.独特的BidiMap方法

Now that we’ve explored the different implementations let’s look at methods that are unique to the interface.

现在我们已经探索了不同的实现,让我们来看看这个接口所特有的方法。

The put() inserts a new key-value entry into the map. Note that if the value of the new entry matches the value of any existing entry, the existing entry will be removed in favor of the new entry.

put() 在map中插入一个新的键值条目。请注意,如果新条目的值与任何现有条目的值相匹配,现有条目将被删除,以支持新条目。

The method returns the removed old entry or null if there’s none:

该方法返回被删除的旧条目,如果没有,则返回null

BidiMap<String, String> map = new DualHashBidiMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
assertEquals(map.size(), 2);

The inverseBidiMap() reverses the key-value pair of a BidiMap. This method returns a new BidiMap where the keys have become the values and vice-versa. This operation can be very useful in translation and dictionary applications:

inverseBidiMap()将一个BidiMap的键值对进行反转。该方法返回一个新的BidiMap,其中的键变成了值,反之亦然。这个操作在翻译和字典应用中非常有用。

BidiMap<String, String> rMap = map.inverseBidiMap();
assertTrue(rMap.containsKey("value1") && rMap.containsKey("value2"));

The removeValue() is used to remove a map entry by specifying a value, instead of a key. This is an addition to Map implementations found in the java.util package:

removeValue()用于通过指定一个值,而不是一个键来删除一个地图条目。这是在java.util包中找到的Map实现的补充。

map.removeValue("value2");
assertFalse(map.containsKey("key2"));

We can get the key mapped to a particular value in BidiMap with the getKey(). The method returns null if no key is mapped onto the specified value:

我们可以通过getKey()获得映射到BidiMap中某个特定值的键。如果没有键映射到指定的值,该方法返回null

assertEquals(map.getKey("value1"), "key1");

5. Conclusion

5.结论

This quick tutorial provided a look into the Apache Commons Collections library – specifically at BidiMap, its implementations and idiosyncratic methods.

这个快速教程提供了对Apache Commons集合库的了解–特别是对BidiMap、其实现和特异性方法的了解。

The most exciting and distinguishing feature of BidiMap is its ability to look up and manipulate entries via keys as well as values.

BidiMap最令人激动和与众不同的特点是它能够通过键和值来查询和操作条目。

As always, code snippets are available over on GitHub.

一如既往,代码片段可在GitHub上获得。

Next »

A Guide to Apache Commons Collections CollectionUtils

« Previous

Apache Commons Collections OrderedMap