如何删除嵌套复杂的链接hashmap中存在的空对象和空值?
我有一个复杂的嵌套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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以声明一个变量,以存储
removenull()
方法的结果在IF语句中,该语句正在对该方法进行递归调用。然后,您在将其添加到ResultLinkedHashmap
之类之前,请检查返回的LinkedHashmap
是否不是空的: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 returnedLinkedHashMap
is not empty before adding it to the resultLinkedHashMap
like:一个简单的空隙方法:
并使用
removenull(test);
调用它A simple void method:
And call it with
removeNull(test);
根据您的尝试,我可以提出一些补充:
一旦您从 block内部从递归中返回后,您就可以
计算
用于映射的空值&amp;删除是否包含空值。Based on your attempt , I can suggest some addition :
Once you returned from recursion inside
if
block , you cancompute
for empty values of mapping & remove if it contains empty values.