Java:使用entrySet()迭代嵌套的hashmap
我正在尝试使用entrySet()迭代嵌套hashmap。多重哈希映射称为数据,我试图在它包含的许多哈希映射中搜索一个值。然而,尽管看起来我找到了正确的位置,但它与我的搜索词不匹配。当我打印这些值时,我看到搜索词确实是当前循环中hashmap中的值之一。这是密码-Java:使用entrySet()迭代嵌套的hashmap,java,hashmap,Java,Hashmap,我正在尝试使用entrySet()迭代嵌套hashmap。多重哈希映射称为数据,我试图在它包含的许多哈希映射中搜索一个值。然而,尽管看起来我找到了正确的位置,但它与我的搜索词不匹配。当我打印这些值时,我看到搜索词确实是当前循环中hashmap中的值之一。这是密码- HashMap<String,HashMap<String,String>> data = driver.data.get(dsName); for (int i = 0; i < arrWhereV
HashMap<String,HashMap<String,String>> data = driver.data.get(dsName);
for (int i = 0; i < arrWhereValues.length; i++) {
String value = null;
for (Entry<String, HashMap<String, String>> entry : data.entrySet()) {
System.out.println("VALUE:::" +entry.getValue());
System.out.println("SEARCH FOR::"+arrWhereValues[i]);
if(arrWhereValues[i].equals(entry.getKey())){
value = entry.getKey();
System.out.println("Value in case 1::" +value);
}
else if(entry.getValue().containsValue(arrWhereValues[i])){ //Why doesn't it enter here???
System.out.println("atleast this much was correct!!");
System.out.println(entry.getValue().entrySet());
for (Entry<String, String> v : entry.getValue().entrySet()) { //Find a better way of doing this.
if(v.getValue().contains(arrWhereValues[i])){
value = v.getKey();
System.out.println("Value in case 2 ::"+value);
}
}
}
}
我做错了什么?使用类似此方法的解决方案打印所有哈希映射的内容:
可能有些键/值不是您所期望的-请参阅Alex的评论,“Testing User”似乎有一个尾随空格。另外,“Testing User”似乎是嵌套hashmap中的一个键,而不是搜索它的外部hashmap中的键。您确定name=“Testing User”的值没有尾随空格吗?因为,对我来说似乎是这样。你能发布什么是需求,而不是一堆没有显示你的需求的代码吗want@alex:是的,这就是问题所在。我纠正了它。谢谢。@Reddy:这就是我正在编写的代码。因为我不明白我可能做错了什么,我在这里问了这个问题。在我解释之后,我认为代码会有意义。如果没有,我很抱歉。作为对“//Find a better way of this”注释的回应,您可以只执行一次:myString=myHashMap.get(myKey),然后检查myString是否为null(如果没有找到值,则为null)。这是HashMap设计的查找类型!
VALUE:::{name=Testing User , slug_primary=null, slug_secondary=null}
SEARCH FOR::Testing User