Java集合 - 唯一键和唯一值 [英] 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?
推荐答案
BiMap 来自 Google Guava 看起来很适合你。
The BiMap from Google Guava looks like it will suit you.
bimap(或双向地图保留其值的唯一性及其键的唯一性。此约束使bimaps支持反向视图,这是另一个bimap,包含与此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.
或 BidiMap from Apache Commons Collections :
定义允许在键和值之间进行双向查找的映射。
Defines a map that allows bidirectional lookup between key and values.
此扩展 Map
表示一个映射,其中键可以查找值,值可以查找缓解。此接口扩展 Map
,因此可以在需要地图的任何地方使用。该界面提供了一个反向地图视图,可以完全访问 BidiMap
的两个方向。
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
.
这篇关于Java集合 - 唯一键和唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!