如何在java中创建一个2路地图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3430170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a 2 way map in java
提问by sekmet64
I need a data structure to store string-int value pairs in an 1:1 relationship, and being able too look up from either way their counterpart.
我需要一个数据结构来以 1:1 的关系存储 string-int 值对,并且也能够从任何一种方式查找它们的对应物。
I wrote a class with a Hashtable and a String array and stored the data 2 times and used the built in functions for lookup.
我写了一个带有 Hashtable 和 String 数组的类,并存储了 2 次数据并使用内置函数进行查找。
My question is that is there a nicer way to accomplish this? And by nicer I mean being efficient and not storing the data 2 times, and preferably without writing a ton of code either :P.
我的问题是有没有更好的方法来实现这一点?更好的是我的意思是高效并且不存储数据 2 次,最好不要编写大量代码:P。
采纳答案by coobird
It seems like you may be looking for a bimap.
看起来您可能正在寻找双图。
The Google Collections (now a part of Guava) contains an BiMap
interface with a few implementations.
Google Collections(现在是Guava的一部分)包含一个BiMap
带有一些实现的接口。
From the BiMap
documentation:
从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 能够支持“反向视图”,这是另一个 bimap,包含与此 bimap 相同的条目,但具有相反的键和值。
The BiMap.inverse
method appears to return a Map
with the values as the keys, and the keys as the values, so that Map
can be used to call get
on the value and retrieve a key.
该BiMap.inverse
方法似乎返回一个Map
以值作为键,键作为值的方法,因此Map
可用于调用get
该值并检索一个键。
In addition the Map
returned by inverse
is a view of the underlying data, so it does not have to make extra copies of the original data.
另外Map
返回的byinverse
是底层数据的视图,所以不需要对原始数据做额外的拷贝。
From the BiMap.inverse
method documentation:
从BiMap.inverse
方法文档:
Returns the inverse view of this bimap, which maps each of this bimap's values to its associated key. The two bimaps are backed by the same data; any changes to one will appear in the other.
返回此 bimap 的逆视图,它将此 bimap 的每个值映射到其关联的键。两个 bimap 由相同的数据支持;对一个的任何更改都会出现在另一个中。
回答by jqno
Google Guavahas a BiMapthat does what you want.
Google Guava有一个BiMap 可以满足您的需求。
回答by Dave Kirby
Create a hashmap that maps Object to Object - then you can use the same map to store String -> Integer and Integer -> String.
创建一个将对象映射到对象的哈希图 - 然后您可以使用相同的映射来存储字符串 -> 整数和整数 -> 字符串。
When you add a string/int pair just add it both ways to the same map.
当您添加一个字符串/整数对时,只需将其以两种方式添加到同一个地图中。
回答by Gopi
You can do a simple implementation like this. Please note that the data is not copied in this implementation. Only the references are ! I have added implementation for add and get. remove and other required method are left as exercise :)
你可以做一个像这样的简单实现。请注意,在此实现中不会复制数据。仅供参考!我已经添加了添加和获取的实现。删除和其他必需的方法留作练习:)
public class TwoWayHashmap<K extends Object, V extends Object> {
private Map<K,V> forward = new Hashtable<K, V>();
private Map<V,K> backward = new Hashtable<V, K>();
public synchronized void add(K key, V value) {
forward.put(key, value);
backward.put(value, key);
}
public synchronized V getForward(K key) {
return forward.get(key);
}
public synchronized K getBackward(V key) {
return backward.get(key);
}
}
And ofcourse its applications responsibility to ensue even the 'values' are unique. Example usage:
当然,即使是“价值观”,它的应用责任也是独一无二的。用法示例:
TwoWayHashmap twmap = new TwoWayHashmap<String, String>();
twmap.add("aaa", "bbb");
twmap.add("xxx", "yyy");
System.out.println(twmap.getForward("xxx"));
System.out.println(twmap.getBackward("bbb"));
回答by Alex B
Apache Commons also includes the BidiMap(Bi Directional Map).
Apache Commons 还包括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 的两个方向。
回答by SohailAziz
Using Guava,
使用番石榴,
HashBiMap<String, String> map = HashBiMap.create();
map.put("name", "Sohail");
map.put("country", "Pakistan");
Log.d("tag", "name is " + map.get("name"));
BiMap<String, String>invmap= map.inverse();
Log.d("tag", "Pakistan is a " + invmap.get("Pakistan"));
read complete tutorial here.