[英]Java Collection - Unique Key and Unique Value
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?
是否有现成的数据结构可以做到这一点?
The BiMap from Google Guava looks 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 BidiMap from 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.这个扩展的
Map
表示一个映射,其中一个键可以查找一个值,一个值可以同样轻松地查找一个键。 This interface extendsMap
and so may be used anywhere a map is required.该接口扩展了
Map
,因此可以在需要地图的任何地方使用。 The interface provides an inverse map view, enabling full access to both directions of theBidiMap
.该界面提供了反向地图视图,可以完全访问
BidiMap
两个方向。
You can use BiMap from Eclipse Collections (formerly GS Collections).您可以使用Eclipse Collections (以前称为 GS Collections)中的BiMap 。
BiMap
is a map that allows users to perform lookups from both directions. BiMap
是一种允许用户从两个方向执行查找的地图。 Both the keys and the values in a BiMap are unique. BiMap 中的键和值都是唯一的。
The main implementation is HashBiMap
.主要实现是
HashBiMap
。
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()
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()
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 的提交者。
The accepted answer mentions BiMap
, but it's become more up-to-date with the Google Guava libraries.接受的答案提到了
BiMap
,但它与谷歌番石榴库变得更加最新。
A
BiMap<K, V>
is aMap<K, V>
thatBiMap<K, V>
是Map<K, V>
- allows you to view the "inverse"
BiMap<V, K>
withinverse()
允许您使用
inverse()
查看“反向”BiMap<V, K>
- ensures that values are unique, making
values()
aSet
确保值是唯一的,使
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:这个对象的一些注意事项:
IllegalArgumentException
.IllegalArgumentException
。 You can use forcePut(key, value)
, but that will override the existing key-value pair .forcePut(key, value)
,但这将覆盖现有的键值对。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.