在 java 中的链接哈希图中查找特定键的值

Find a value for a specific key in a linked hashmap in java

是否有更简单的方法来查找 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");
    }
}
LinkedHashMap<String , Object> newmap = (LinkedHashMap<String, Object>) entry.getValue();
String newType = (String) newmap.getOrDefault("jcr:primaryType", "");

因为你的默认结果是"",你可以

String newType = newmap.getOrDefault("jcr:primaryType", "");

如果我们谈论的是纯 Map 透视但在 JCR 或 Magnolia 中,以上所有答案都是正确的;你可以使用 javax.jcr.Node#getProperty

要查询特定 属性 的 Node 对象,可以使用; javax.jcr.Node#hasProperty