Java 将Map里面的 ‘空’值 移除

java 复制代码
    public static  void main(String[] args) {

        Map<String, Object> map=new HashMap<>();
        map.put("A","");
        map.put("B",new ArrayList<>());
        map.put("C",null);
        map.put("D",123);
        System.out.println(map.toString());
        Map<String, Object> mapNew = removeEmpty(map);
        System.out.println(mapNew.toString());
    }

如: 对于 空的list ,以及 双引号的 字符串,都认为是空。

移除方法 :

java 复制代码
    private  static Map<String, Object> removeEmpty(Map<String, ?> paramsMap) {
        return paramsMap.entrySet().stream()
                .filter(entry -> entry.getValue() != null)
                .filter(entry -> !(entry.getValue() instanceof String) || StringUtils.isNotEmpty((String) entry.getValue()))
                .filter(entry -> !(entry.getValue() instanceof List) || CollectionUtils.isNotEmpty((List<?>) entry.getValue()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    }

效果示例 :

相关推荐
灿飞屋15 分钟前
Kotlin与Java虚拟机四:函数
java·python·kotlin·安卓·android jetpack
阡陌数智1 小时前
Claude API新版本重大变更与安全升级指南
java·linux·安全
学长毕设3 小时前
基于SpringBoot的野生动物公益保护平台设计与实现-附源码
java·spring boot·后端
SL_staff3 小时前
离散制造排程破局:JVS-APS 四步闭环落地实践(开发者视角)
java·产品·全栈
这个昵称不容易4 小时前
SpringBoot 美容院管理系统:232个Java文件315个接口,5套角色从预约到发货一整条链路
java·springboot·毕设
装不满的克莱因瓶4 小时前
【SpringAI Alibaba】ReactAgent记忆管理MessageTrimmingHook与MessageDeletionHook
java·人工智能·ai·ai编程·springai·springaialibaba
文丑颜不良啊5 小时前
LeetCode2241~2260题目解析
java·leetcode
文丑颜不良啊5 小时前
LeetCode2321~2340题目解析
java·leetcode
驭渊的小故事5 小时前
Spring AOP 核心概念与通知类型详解
java·后端·spring
vipxieliang5 小时前
ValidX vs 手写验证代码:开发效率对比实测
java·spring boot·后端