HashMaps遍历的HashMap

发布于 2024-10-05 20:49:53 字数 834 浏览 0 评论 0 原文

在Java中,我试图检索一个HashMap,其对象为:HashMap

我实现了一个递归函数,该函数返回使用给定键找到的 HashMap ,如果未找到该键,则返回 null

这是函数:

public static HashMap<String, Object> getHashMap(HashMap<String, 
                                      Object> map, String key)
{
    for (Map.Entry<String, Object> entry : map.entrySet()) {
     if (entry.getValue().getClass().getName() == "java.util.HashMap") {
         if (entry.getKey() == key) 
          return (HashMap<String, Object>) entry.getValue();
         return getHashMap((HashMap<String, Object>) entry.getValue(), key);
     }
    }
    return null;
}

它仅适用于第一项。如何遍历 HashMap 的 Hashmap?什么是更好的方法?

In Java, I'm trying to retrieve a HashMap<String, Object> that has the Object which is: HashMap<String, Object>.

I implemented a recursive function that returns either the HashMap<String, Object> found with the given key, or null if the key wasn't found.

Here is the function:

public static HashMap<String, Object> getHashMap(HashMap<String, 
                                      Object> map, String key)
{
    for (Map.Entry<String, Object> entry : map.entrySet()) {
     if (entry.getValue().getClass().getName() == "java.util.HashMap") {
         if (entry.getKey() == key) 
          return (HashMap<String, Object>) entry.getValue();
         return getHashMap((HashMap<String, Object>) entry.getValue(), key);
     }
    }
    return null;
}

It only works for the first item. How do I traverse a Hashmap of HashMaps? What is a better approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

零度℉ 2024-10-12 20:49:53

不要在此处立即返回值:

return getHashMap((HashMap<String, Object>) entry.getValue(), key);

您需要首先检查它是否不为 null,然后才返回它。否则你应该继续搜索:

HashMap<String, Object> result = getHashMap((HashMap<String, Object>) entry.getValue(), key);
if (result != null)
  return result;

Instead of returning the value immediately here:

return getHashMap((HashMap<String, Object>) entry.getValue(), key);

you want to first check if it is not null, and return it only then. Otherwise you should just continue searching:

HashMap<String, Object> result = getHashMap((HashMap<String, Object>) entry.getValue(), key);
if (result != null)
  return result;
长安忆 2024-10-12 20:49:53

其一,不要对字符串使用 ==。而是使用 equals 方法。

另一方面,我更喜欢使用 instanceof ,例如 if (myObject instanceof java.util.Map) { ... } 这样,如果以后您决定更改,您的地图不必是 HashMap它。

For one, don't use == for Strings. Instead use the equals method.

For another, I prefer to do instanceof such as if (myObject instanceof java.util.Map) { ... } this way your map doesn't have to be a HashMap if later you decide to change it.

稚气少女 2024-10-12 20:49:53

另一种方法。尝试使用 HashMap> main 作为您的“外部”HashMap,然后您将搜索 key

Object value = main.get(key);
if (value == null) {
    for (HashMap<String, Object> inner : main.values()) {
        value = inner.get(key);
        if (value != null) {
            break;
        }
    }
}

Another approach. Try use HashMap<String, HashMap<String, Object>> main for your 'outer' HashMap, then you'll search for key:

Object value = main.get(key);
if (value == null) {
    for (HashMap<String, Object> inner : main.values()) {
        value = inner.get(key);
        if (value != null) {
            break;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文