代码写法:
private static Map<String, Object> removeEmpty(Map<String, Object> map) {
return map.entrySet().stream()
.filter(entry -> entry.getValue() != null)
.filter(entry -> !(entry.getValue() instanceof String) || !StringUtils.isEmpty(entry.getValue()))
.filter(entry -> !(entry.getValue() instanceof List) || CollectionUtils.isNotEmpty((List) entry.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
测试效果:
public static void main(String[] args) {
Map<String, Object> map=new HashMap<>();
map.put("test1",null);
map.put("test2",new ArrayList<>());
map.put("test3","你好");
System.out.println("移除前:"+map);
System.out.println("移除后:"+removeEmpty(map));
}
效果:
移除前:{test2=[], test3=你好, test1=null}
移除后:{test3=你好}