如何删除嵌套复杂的链接hashmap中存在的空对象和空值?

发布于 2025-01-30 18:08:29 字数 1829 浏览 4 评论 0原文

我有一个复杂的嵌套linkedhashmap我为此分配了来自另一个应用程序的值。由于输入JSON中可能缺少该值,因此某些字段可能具有零值。

我正在寻找一种方法来省略所有无效&我的复杂/嵌套LinkedHashmap中存在的空值。如果它是直接键,则值linkedhashmap,则该过程非常简单,但是就我而言,LinkedHashmap值可以在其中包含另一个LinkedHashMap。

即使在嵌套的LinkedHashMap中,是否有任何简单,最佳的Java 8流删除所有空/空值的方法?

以下是我拥有的示例LinkedHashmap结构:

package io.template;

import java.util.LinkedHashMap;

public class TemplateNodeMap extends LinkedHashMap {

    public TemplateNodeMap() {
        put("type", null);
        
        put("step", null);

        put("error", new LinkedHashMap<>() {{
            put("time", null);
            put("errorId", new LinkedHashMap<>() {{
                put("correctId", null);
            }});
        }});

        put("sensor", new LinkedHashMap<>() {{
            put("metadata", new LinkedHashMap<>() {{
                put("time", null);
                put("startTime", null);
            }});

            put("report", new LinkedHashMap<>() {{
                put("type", null);
                put("device", null);
                put("deviceMetadata", null);
            }});
        }});
    }
}

当前,我编写了类似的递归代码:

public LinkedHashMap removeNull(LinkedHashMap map) {
    LinkedHashMap templateNodeMap = new LinkedHashMap();

    map.forEach((key, value) -> {
        if (value != null && value instanceof LinkedHashMap) {
            templateNodeMap.put(key, removeNull((LinkedHashMap) value));
        } else if (value != null) {
            templateNodeMap.put(key, value);
        }
    });
    return templateNodeMap;
}

上面的代码适用于null,但没有删除空值。对于某些字段,我将值作为sensor = {metadata = {},report = {}}}},如果没有任何值,我想完全删除字段。我不希望获得空对象{}

有人可以让我知道是否有一种方法在Java流中或以递归方法进行操作。提前致谢。

I have a complex nested LinkedHashMap for which I am assigning the values coming from another application. Some of the fields can have Null values since the values may be missing in input JSON.

I am looking for an approach to omit all the Null & Empty values present within my Complex/Nested LinkedHashMap. If it's a direct key, value LinkedHashMap then the process is quite straightforward forward but in my case, the LinkedHashMap value can have another LinkedHashmap within it.

Is there any simple, optimal Java 8 streams way to remove all empty/null values even within nested LinkedHashMap?

Following is the sample LinkedHashMap structure I have:

package io.template;

import java.util.LinkedHashMap;

public class TemplateNodeMap extends LinkedHashMap {

    public TemplateNodeMap() {
        put("type", null);
        
        put("step", null);

        put("error", new LinkedHashMap<>() {{
            put("time", null);
            put("errorId", new LinkedHashMap<>() {{
                put("correctId", null);
            }});
        }});

        put("sensor", new LinkedHashMap<>() {{
            put("metadata", new LinkedHashMap<>() {{
                put("time", null);
                put("startTime", null);
            }});

            put("report", new LinkedHashMap<>() {{
                put("type", null);
                put("device", null);
                put("deviceMetadata", null);
            }});
        }});
    }
}

Currently, I have written a recursive code something like this:

public LinkedHashMap removeNull(LinkedHashMap map) {
    LinkedHashMap templateNodeMap = new LinkedHashMap();

    map.forEach((key, value) -> {
        if (value != null && value instanceof LinkedHashMap) {
            templateNodeMap.put(key, removeNull((LinkedHashMap) value));
        } else if (value != null) {
            templateNodeMap.put(key, value);
        }
    });
    return templateNodeMap;
}

The above code is working for null but it does not remove the empty values. For some fields, I am getting the value as sensor={metadata={},report={}}}, I would like to remove fields altogether if it does not have any values. I do not wish to get even the empty object {}.

Can someone please let me know if there is a way to do it in Java Streams or in a recursive approach. Thanks in advance.

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

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

发布评论

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

评论(3

夏末染殇 2025-02-06 18:08:29

您可以声明一个变量,以存储removenull()方法的结果在IF语句中,该语句正在对该方法进行递归调用。然后,您在将其添加到Result LinkedHashmap之类之前,请检查返回的LinkedHashmap是否不是空的:

public LinkedHashMap removeNull(LinkedHashMap map) {
    LinkedHashMap templateNodeMap = new LinkedHashMap();

    map.forEach((key, value) -> {
        if (value != null && value instanceof LinkedHashMap) {
            LinkdedHashMap hashMap = removeNull((LinkedHashMap) value);
            if(!hashMap.isEmpty()) {
               templateNodeMap.put(key, hashMap);
            }
        } else if (value != null) {
            templateNodeMap.put(key, value);
        }
    });
    return templateNodeMap;
}

You can declare a variable to store the results of your removeNull() method inside the if statement that is making a recursive call to the method. You then check if the returned LinkedHashMap is not empty before adding it to the result LinkedHashMap like:

public LinkedHashMap removeNull(LinkedHashMap map) {
    LinkedHashMap templateNodeMap = new LinkedHashMap();

    map.forEach((key, value) -> {
        if (value != null && value instanceof LinkedHashMap) {
            LinkdedHashMap hashMap = removeNull((LinkedHashMap) value);
            if(!hashMap.isEmpty()) {
               templateNodeMap.put(key, hashMap);
            }
        } else if (value != null) {
            templateNodeMap.put(key, value);
        }
    });
    return templateNodeMap;
}
小傻瓜 2025-02-06 18:08:29

一个简单的空隙方法:

public static void removeNull(Map<?, ?> map) {
    map.values().forEach(e -> {
        if (e instanceof Map)
            removeNull((Map<?, ?>) e);
    });
    map.values().removeIf(e -> e == null || e instanceof Map && ((Map<?, ?>) e).isEmpty());
}

并使用removenull(test);调用它

A simple void method:

public static void removeNull(Map<?, ?> map) {
    map.values().forEach(e -> {
        if (e instanceof Map)
            removeNull((Map<?, ?>) e);
    });
    map.values().removeIf(e -> e == null || e instanceof Map && ((Map<?, ?>) e).isEmpty());
}

And call it with removeNull(test);

梦途 2025-02-06 18:08:29

根据您的尝试,我可以提出一些补充:

map.forEach((key, value) -> {
        if (value != null && value instanceof LinkedHashMap) {
            templateNodeMap.put(key, removeNull((LinkedHashMap) value));
            templateNodeMap.compute(key, (x,y)->(y.isEmpty()) ? null : y); 
        } else if (value != null) {
            templateNodeMap.put(key, value);
        }
    });

一旦您从 block内部从递归中返回后,您就可以计算用于映射的空值&amp;删除是否包含空值。

Based on your attempt , I can suggest some addition :

map.forEach((key, value) -> {
        if (value != null && value instanceof LinkedHashMap) {
            templateNodeMap.put(key, removeNull((LinkedHashMap) value));
            templateNodeMap.compute(key, (x,y)->(y.isEmpty()) ? null : y); 
        } else if (value != null) {
            templateNodeMap.put(key, value);
        }
    });

Once you returned from recursion inside if block , you can compute for empty values of mapping & remove if it contains empty values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文