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

效果示例 :

相关推荐
EXI-小洲7 小时前
Java 操作 Word:字符串替换、图片插入、动态生成表格与API接口下载
java·开发语言·spring boot·word
2601_961901708 小时前
SpringBoot使用Nacos进行application.yml配置管理
java·spring boot·spring
何以解忧,唯有..9 小时前
LangChain 工具调用(Tool Calling)实战指南
java·前端·langchain
QQ_216962909610 小时前
【源码编号:project79475】SpringBoot校内二手交易平台:商品发布、分类检索、留言交流、订单管理全流程实战
java·spring boot·后端
前端 贾公子11 小时前
第09章:上下文与记忆 (2)
java·服务器·前端
2601_9622035111 小时前
Java进阶07集合(续)
java·开发语言
郑州光合科技余经理11 小时前
本地生活服务系统:模块边界与结算字段怎么拆
java·开发语言·前端·后端·系统架构·uni-app·php
roman_日积跬步-终至千里11 小时前
【数据治理(6)】DataOps 不是调度工具,而是让数据产品长期可信的运行机制
java·服务器·网络
落魄大学生之流水线上谋生计12 小时前
LangGraph 基本用法指南
java·windows·python·langchain
2601_9621284112 小时前
SpringBoot篇(缓存层)
java·spring boot·缓存