Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java集合-唯一键和唯一值_Java_Collections - Fatal编程技术网

Java集合-唯一键和唯一值

Java集合-唯一键和唯一值,java,collections,Java,Collections,我需要一个集合,可以根据键查找值,反之亦然。每个值都有一个键,每个键都有一个值。有没有现成的数据结构可以做到这一点 这件衣服看起来很适合你 bimap(或“双向映射”)是保留其值及其键唯一性的映射。此约束使bimap支持“反向视图”,这是另一个bimap,包含与此bimap相同的条目,但具有反向键和值 或来自: 定义允许在键和值之间进行双向查找的映射 此扩展的映射表示一个映射,其中键可以查找值,值可以同样轻松地查找键。此接口扩展了Map,因此可以在需要Map的任何地方使用。该界面提供了反向地图视

我需要一个集合,可以根据键查找值,反之亦然。每个值都有一个键,每个键都有一个值。有没有现成的数据结构可以做到这一点

这件衣服看起来很适合你

bimap(或“双向映射”)是保留其值及其键唯一性的映射。此约束使bimap支持“反向视图”,这是另一个bimap,包含与此bimap相同的条目,但具有反向键和值

或来自:

定义允许在键和值之间进行双向查找的映射

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

您可以使用from(以前是GS集合)

BiMap
是一种允许用户从两个方向执行查找的地图。BiMap中的键和值都是唯一的

主要实现是
HashBiMap

inverse()

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));
forcePut()

其行为类似于
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);
MutableBiMap biMap=HashBiMap.newMap();
biMap.forcePut(1,“1”);//行为类似于常规put()
biMap.forcePut(1,“1”);//无效
biMap.put(1,“2”);//将[1,“1”]对替换为[1,“2”]
biMap.forcePut(2,“2”);//放置前移除[1,“2”]对
Assert.assertFalse(biMap.containsKey(1));
Assert.assertEquals(HashBiMap.newWithKeysValues(2,“2”),biMap);

注意:我是Eclipse集合的提交人。

公认的答案提到了
BiMap
,但它已经成为Google Guava库的一部分

BiMap

  • 允许您使用
    inverse()
  • 确保值是唯一的,使
    values()
    a
    Set
因此,您可以得到如下代码:

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
final BiMap BiMap=HashBiMap.create();
biMap.put(“单词”,1);
biMap.put(“α”,2);
System.out.println(biMap.get(“word”);//印刷品1
System.out.println(biMap.inverse().get(1));//印刷字
此对象的一些注意事项:

  • 您将无法添加非唯一值,否则将得到一个
    IllegalArgumentException
    。您可以使用
    forcePut(键、值)
    ,但这样做是可行的
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