从 Java 哈希表中的值中检索键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15094977/
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
Retrieving key from value in hashtable in Java
提问by user2094103
I am a new bie to the world of collections but still, In my below class I have hashtable as I have to choose hashtable and I want to retrieve the key basis on the value , below is my class..
我是收藏世界的新手,但是,在我下面的课程中,我有哈希表,因为我必须选择哈希表,并且我想根据值检索关键基础,下面是我的课程..
public class KeyFromValueExample
{
public static void main(String args[])
{
Hashtable table = new Hashtable();
table.put("Sony", "Bravia");
table.put("Samsung", "Galaxy");
table.put("Nokia", "Lumia");
System.out.println("does hash table has Lumia as value : " + table.containsValue("Lumia"));
System.out.println("does hash table Lumia as key : " + table.containsKey("Lumia"));
//finding key corresponding to value in hashtable - one to one mapping
String key= null;
String value="Lumia";
for(Map.Entry entry: table.entrySet()){
if(value.equals(entry.getValue())){
key = entry.getKey();
break; //breaking because its one to one map
}
}
System.out.println("got key from value in hashtable key: "+ key +" value: " + value);
//finding key corresponding to value in hashtable - one to many mapping
table.put("HTC", "Lumia");
Set keys = new HashSet();
for(Map.Entry entry: table.entrySet()){
if(value.equals(entry.getValue())){
keys.add(entry.getKey()); //no break, looping entire hashtable
}
}
System.out.println("keys : " + keys +" corresponding to value in hash table: "+ value);
Output :-
输出 :-
does hash table has Lumia as value : true
does hash table has Lumia as key : false
got key from value in hashtable key: Nokia value: Lumia
keys : [Nokia, HTC] corresponding to value in hash talbe: Lumia
Now please advis is there any other better way to achieve the same thing, Please advise if other better alternatives are there.
现在请告知是否有其他更好的方法可以实现相同的目标,请告知是否有其他更好的替代方法。
回答by SDJMcHattie
That looks pretty obvious what it's doing to me. I'm not sure there's a more efficient way of doing it, but perhaps if you need to look up values to find the keys quite regularly, then the type of map you are using is wrong. I would suggest you turn the values into keys and make a Map<String, Set<String>>
instead
这对我来说很明显。我不确定是否有更有效的方法,但也许如果您需要定期查找值以找到键,那么您使用的地图类型是错误的。我建议您将值转换为键并Map<String, Set<String>>
改为
回答by Alexander Pogrebnyak
If your map has both unique keys and values, consider using BiMap from Guava collections -> http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/BiMap.html
如果您的地图同时具有唯一键和值,请考虑使用 Guava 集合中的 BiMap -> http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/BiMap.html
With that library, you can just use simple containsKey
and containsValue
tests.
使用该库,您可以只使用简单containsKey
和containsValue
测试。
回答by mkirci
I do not know if it is what you want, but why don't you reverse your key and values? So, you can use
我不知道这是不是你想要的,但你为什么不反转你的键和值呢?所以,你可以使用
table.put("Lumia", new List<String>("HTC", "Nokia"));
something like that if above is legal.
类似的东西,如果以上是合法的。
回答by Prasanthi Dev
Is there a way to find the key of a particular value in HashTable without iterator??
like String getvalue(key); ??
for(Map.Entry entry: table.entrySet()){
if(value.equals(entry.getValue())){
keys.add(entry.getKey()); //no break, looping entire hashtable
}
}