[英]How to retrieve a key from nested HashMaps instead of its Value
I am trying to figure out a fast way of how to access the key of a nested hashmap.我正在尝试找出一种快速访问嵌套 hashmap 密钥的方法。
I am quite new to java and i know how to access the value of the innermost hashmap (with the get() method) but I can't figure out how to easily access the key of an inner hashmap.我对 java 很陌生,我知道如何访问最里面的 hashmap 的值(使用 get() 方法),但我不知道如何轻松访问内部 ZDDA7806A4847A51B5940CBD26 的密钥The hashMap has its own class and is defined like this: hashMap 有自己的 class,定义如下:
HashMap<String, HashMap<String, HashMap<Double, Integer>>> data;
data = new HashMap<String, HashMap<String, HashMap<Double, Integer>>>();
I can perfectly get the Value of the inner most HashMap by using this get() method I defined within the class:通过使用我在 class 中定义的 get() 方法,我可以完美地获得最里面的 HashMap 的值:
int get(String a, String b, Double c){
if (data.containsKey(a)){
if (data.get(a).containsKey(b)){
if (data.get(a).get(b).containsKey(c){
return data.get(a).get(b).get(c);
}
}
}
return 0;
Now i want to define a method getk() for this class and HashMap that allows me to retrieve the key of the inner HashMap by entering the first two Keys like this:现在我想为此 class 和 HashMap 定义一个方法 getk() ,它允许我通过输入前两个密钥来检索内部 HashMap 的密钥,如下所示:
double getk(String a, String b){
if (data.containsKey(a)){
if (data.get(a).containsKey(b)){
return ***I don't know what to put here***;
}
}
return 0;
}
Edit additional info: The key "c" I want to get has a 1:1 relationship with key "b".编辑附加信息:我想要获得的键“c”与键“b”具有 1:1 的关系。 Is there a possibility to easily solve this?有没有可能轻松解决这个问题?
Thanks a lot for your time and help;)非常感谢您的时间和帮助;)
You cannot get only one key of the inner Hashmap.您不能只获得内部 Hashmap 的一个键。 Because Hashmap contains as Keys a set of keys.因为 Hashmap 包含一组密钥作为密钥。 You can get the list of keys by calling:您可以通过调用获取键列表:
map.keySet();
Your IDE should helps you find that.您的 IDE 应该可以帮助您找到它。
[EDIT] [编辑]
If you have a 1:1 relationship, just used a Pair Wrapper Object than HashMap.如果您有 1:1 的关系,只需使用 Pair Wrapper Object 而不是 HashMap。 like this one:像这个:
public class KeyValue {
private double key;
private Integer value;
public KeyValue(double key, Integer value) {
this.key = key;
this.value = value;
}
public double getKey() {
return key;
}
public void setKey(double key) {
this.key = key;
}
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
}
You wil be able to get your key by calling getKey() of this Object.您可以通过调用此 Object 的 getKey() 来获取您的密钥。
You can call method keySet() on hashmap onject to get a Set of all keys in hashmap.您可以在 hashmap onject 上调用方法 keySet() 来获取 hashmap 中所有密钥的集合。
myMap.keySet(); myMap.keySet();
https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#keySet-- https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#keySet--
Edit:编辑:
Sorry i misunderstood your question.对不起,我误解了你的问题。 All that you have to do is just return the following您所要做的就是返回以下内容
data.get(a).get(b).keySet().iterator().next();
As the b hash set has only one key c, by iterating the first element will return the key c由于 b hash 集合只有一个键 c,通过迭代第一个元素将返回键 c
keySet()
method returns set of all the keys. keySet()
方法返回所有键的集合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.