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));
    }

效果示例 :

相关推荐
郑州光合科技余经理1 小时前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
大大水瓶2 小时前
Tomcat
java·tomcat
dustcell.2 小时前
haproxy七层代理
java·开发语言·前端
游离态指针2 小时前
以为发消息=下单成功?RabbitMQ从0到秒杀实战的完整踩坑笔记
java
BD_Marathon2 小时前
工厂方法模式
android·java·工厂方法模式
玹外之音3 小时前
Spring AI MCP 无状态服务器实战:构建AI智能用户管理系统
java·后端
java干货3 小时前
Redis 的 ZipList 是什么?它是怎么解决内存碎片问题的?
java
失重外太空啦3 小时前
Tomcat
java·服务器·tomcat
屎到临头想搅便3 小时前
TOMCAT
java·tomcat
Sylvia33.3 小时前
火星数据:解构斯诺克每一杆进攻背后的数字语言
java·前端·python·数据挖掘·数据分析