首页 > 解决方案 > BiMap with uniqueness along the set (key, value)?

问题描述

I've seen that BiMap is for a bidirectional-map, that is link two set. But does it exists a class that ensures unique-ness along with the set keys and values? (one-to-one relationship) With possibly a unique method get(), that is theoretically possible with that. The keys and values have the same class.

Bonus: is BiMap.inverse() complexity constant ?

标签: javaguava

解决方案


确保唯一性以及设置的键和值

键和值具有相同的类。

看来您需要不同的数据结构,而不是BiMap. 您是否考虑过无向Graph1Network2

class Person { String name;}
final var john = new Person("John");
final var jane = new Person("Jane");
final MutableGraph<Person> graph = GraphBuilder.undirected().build();
graph.putEdge(john, jane);
Assertions.assertTrue(graph.hasEdgeConnecting(john, jane));
Assertions.assertTrue(graph.hasEdgeConnecting(jane, john));
Assertions.assertEquals(graph.adjacentNodes(jane), Set.of(john));
Assertions.assertEquals(graph.adjacentNodes(john), Set.of(jane));

推荐阅读