Java Mapping Data Structure
本问题已经有最佳答案,请猛点这里访问。
Possible Duplicate:
Java Hashmap: How to get key from value?
我正在寻找一个Java数据结构(某种地图),我可以在其中对Keys和Values执行查找。 例如,假设我在一组字符串和整数之间有一对一的映射。 调用此对象映射器。 我希望能够执行以下操作:
我认为你正在寻找谷歌番石榴BiMap(或)公共BidiMap。
例:
1 2 3 4 5 6 7 8 9 | BidiMap bidiMap = new DualHashBidiMap( ); bidiMap.put("il","Illinois" ); bidiMap.put("az","Arizona" ); bidiMap.put("va","Virginia" ); // Retrieve the key with a value via the inverse map String vaAbbreviation = bidiMap.inverseBidiMap( ).get("Virginia" ); // Retrieve the value from the key String illinoisName = bidiMap.get("il" ); |
有关BiMap示例,请参阅此文章。
您可以考虑使用Guava的
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.
因此,给定