【发布时间】:2020-01-14 12:25:51
【问题描述】:
有没有更简单的方法可以在 Java 的链接哈希映射中找到特定键的值?
HashMap<String, Object> newmap = (HashMap<String, Object>) entry.getValue();
String newType = "";
//finds out the primaryType for the new node
for (Entry<String, Object> mapentry : newmap.entrySet()) {
if (mapentry.getKey() == "jcr:primaryType") {
newType = (String) newmap.get("jcr:primaryType");
}
}
【问题讨论】:
-
就在您的代码中:
newmap.get("jcr:primaryType")。其余的都是多余的。 -
除了
newmap.get("jcr:primaryType");之外的所有内容在您发布的代码中都毫无意义。此外,您发布的内容也不起作用,因为您正在尝试使用==比较字符串 -
另外,您的代码会创建一个
HashMap,而不是LinkedHashMap。