Java Collection - 独特的关键和独特的价值

时间:2022-12-15 20:48:35

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?

我需要一个可以根据键查找值的集合,反之亦然。对于每个值,都有一个键,每个键都有一个值。是否有可以使用的数据结构?

3 个解决方案

#1


32  

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(或“双向映射”)是一种保留其值及其键值的唯一性的映射。此约束使bimaps支持“反向视图”,这是另一个包含与此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. This interface extends Map and so may be used anywhere a map is required. The interface provides an inverse map view, enabling full access to both directions of the BidiMap.

此扩展Map表示一个映射,其中键可以查找值,值可以轻松地查找键。此接口扩展了Map,因此可以在需要地图的任何地方使用。界面提供逆映射视图,可以完全访问BidiMap的两个方向。

#2


7  

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. Both the keys and the values in a BiMap are unique.

BiMap是一个允许用户从两个方向执行查找的地图。 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的提交者。

#3


1  

The accepted answer mentions BiMap, but it's become more up-to-date with the Google Guava libraries.

接受的答案提到了BiMap,但它与Google Guava库的关系变得更新了。

A BiMap<K, V> is a Map<K, V> that

BiMap 是Map ,v> ,v>

  • allows you to view the "inverse" BiMap<V, K> with inverse()
  • 允许您使用inverse()查看“逆”BiMap ,k>

  • ensures that values are unique, making values() a Set
  • 确保值是唯一的,使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 use forcePut(key, value), but that will override the existing key-value pair.
  • 您将无法添加非唯一值,或者您将获得IllegalArgumentException。您可以使用forcePut(键,值),但这将覆盖现有的键值对。

#1


32  

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(或“双向映射”)是一种保留其值及其键值的唯一性的映射。此约束使bimaps支持“反向视图”,这是另一个包含与此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. This interface extends Map and so may be used anywhere a map is required. The interface provides an inverse map view, enabling full access to both directions of the BidiMap.

此扩展Map表示一个映射,其中键可以查找值,值可以轻松地查找键。此接口扩展了Map,因此可以在需要地图的任何地方使用。界面提供逆映射视图,可以完全访问BidiMap的两个方向。

#2


7  

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. Both the keys and the values in a BiMap are unique.

BiMap是一个允许用户从两个方向执行查找的地图。 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的提交者。

#3


1  

The accepted answer mentions BiMap, but it's become more up-to-date with the Google Guava libraries.

接受的答案提到了BiMap,但它与Google Guava库的关系变得更新了。

A BiMap<K, V> is a Map<K, V> that

BiMap 是Map ,v> ,v>

  • allows you to view the "inverse" BiMap<V, K> with inverse()
  • 允许您使用inverse()查看“逆”BiMap ,k>

  • ensures that values are unique, making values() a Set
  • 确保值是唯一的,使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 use forcePut(key, value), but that will override the existing key-value pair.
  • 您将无法添加非唯一值,或者您将获得IllegalArgumentException。您可以使用forcePut(键,值),但这将覆盖现有的键值对。