Java 集合 - 唯一键和唯一值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/711618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Collection - Unique Key and Unique Value
提问by javacavaj
I need a collection that can lookup a value based on the key and vice versa. For every value there is one key and for every key there is one value. Is there a ready to use data structure out there that does this?
我需要一个可以根据键查找值的集合,反之亦然。每个值都有一个键,每个键都有一个值。是否有现成的数据结构可以做到这一点?
采纳答案by Michael Myers
The BiMapfrom Google Guavalooks like it will suit you.
来自Google Guava的BiMap看起来很适合你。
A bimap (or "bidirectional map") is a map that preserves the uniqueness of its values as well as that of its keys. This constraint enables bimaps to support an "inverse view", which is another bimap containing the same entries as this bimap but with reversed keys and values.
双映射(或“双向映射”)是一种保留其值及其键的唯一性的映射。此约束使 bimap 能够支持“反向视图”,这是另一个 bimap,包含与此 bimap 相同的条目,但具有相反的键和值。
Or the BidiMapfrom Apache Commons Collections:
或者来自Apache Commons Collections的BidiMap:
Defines a map that allows bidirectional lookup between key and values.
This extended
Map
represents a mapping where a key may lookup a value and a value may lookup a key with equal ease. This interface extendsMap
and so may be used anywhere a map is required. The interface provides an inverse map view, enabling full access to both directions of theBidiMap
.
定义允许在键和值之间进行双向查找的映射。
这个扩展
Map
表示一个映射,其中一个键可以查找一个值,一个值可以同样轻松地查找一个键。此接口扩展Map
,因此可以在需要地图的任何地方使用。该界面提供了反向地图视图,可以完全访问BidiMap
.
回答by Craig P. Motlin
You can use BiMapfrom Eclipse Collections(formerly GS Collections).
您可以使用BIMAP从Eclipse的集合(前身为GS集合)。
BiMap
is a map that allows users to perform lookups from both directions. Both the keys and the values in a BiMap are unique.
BiMap
是一种允许用户从两个方向执行查找的地图。BiMap 中的键和值都是唯一的。
The main implementation is HashBiMap
.
主要实现是HashBiMap
.
inverse()
inverse()
BiMap.inverse()
returns a view where the position of the key type and value type are swapped.
BiMap.inverse()
返回一个视图,其中键类型和值类型的位置被交换。
MutableBiMap<Integer, String> biMap =
HashBiMap.newWithKeysValues(1, "1", 2, "2", 3, "3");
MutableBiMap<String, Integer> inverse = biMap.inverse();
Assert.assertEquals("1", biMap.get(1));
Assert.assertEquals(1, inverse.get("1"));
Assert.assertTrue(inverse.containsKey("3"));
Assert.assertEquals(2, inverse.put("2", 4));
put()
put()
MutableBiMap.put()
behaves like Map.put()
on a regular map, except it throws when a duplicate value is added.
MutableBiMap.put()
行为就像Map.put()
在常规地图上一样,除了在添加重复值时抛出。
MutableBiMap<Integer, String> biMap = HashBiMap.newMap();
biMap.put(1, "1"); // behaves like a regular put()
biMap.put(1, "1"); // no effect
biMap.put(2, "1"); // throws IllegalArgumentException
forcePut()
forcePut()
This behaves like MutableBiMap.put()
, but it silently removes the map entry with the same value before putting the key-value pair in the map.
这与 类似MutableBiMap.put()
,但在将键值对放入映射之前,它会默默地删除具有相同值的映射条目。
MutableBiMap<Integer, String> biMap = HashBiMap.newMap();
biMap.forcePut(1, "1"); // behaves like a regular put()
biMap.forcePut(1, "1"); // no effect
biMap.put(1, "2"); // replaces the [1,"1"] pair with [1, "2"]
biMap.forcePut(2, "2"); // removes the [1, "2"] pair before putting
Assert.assertFalse(biMap.containsKey(1));
Assert.assertEquals(HashBiMap.newWithKeysValues(2, "2"), biMap);
Note:I am a committer for Eclipse Collections.
注意:我是 Eclipse Collections 的提交者。
回答by Makoto
The accepted answer mentions BiMap
, but it's become more up-to-datewith the Google Guava libraries.
接受的答案提到了BiMap
,但随着 Google Guava 库的更新,它变得更新了。
A
BiMap<K, V>
is aMap<K, V>
that
- allows you to view the "inverse"
BiMap<V, K>
withinverse()
- ensures that values are unique, making
values()
aSet
A
BiMap<K, V>
是Map<K, V>
那个
- 允许您查看“反”
BiMap<V, K>
与inverse()
- 确保值是唯一的,使得
values()
一个Set
So, you can end up with code like this:
所以,你可以得到这样的代码:
final BiMap<String, Integer> biMap = HashBiMap.create();
biMap.put("word", 1);
biMap.put("alpha", 2);
System.out.println(biMap.get("word")); // prints 1
System.out.println(biMap.inverse().get(1)); // prints word
Some caveats with this object:
这个对象的一些注意事项:
- You won't be able to add non-unique values, or you'll get an
IllegalArgumentException
. You can useforcePut(key, value)
, but that will override the existing key-value pair.
- 您将无法添加非唯一值,否则您将获得
IllegalArgumentException
. 您可以使用forcePut(key, value)
,但这会覆盖现有的键值对。