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

效果示例 :

相关推荐
java1234_小锋1 分钟前
Java高频面试题:SpringBoot为什么要禁止循环依赖?
java·开发语言·面试
2501_9445255416 分钟前
Flutter for OpenHarmony 个人理财管理App实战 - 账户详情页面
android·java·开发语言·前端·javascript·flutter
计算机学姐16 分钟前
基于SpringBoot的电影点评交流平台【协同过滤推荐算法+数据可视化统计】
java·vue.js·spring boot·spring·信息可视化·echarts·推荐算法
Filotimo_32 分钟前
Tomcat的概念
java·tomcat
索荣荣1 小时前
Java Session 全面指南:原理、应用与实践(含 Spring Boot 实战)
java·spring boot·后端
Amumu121381 小时前
Vue Router(二)
java·前端
念越2 小时前
数据结构:栈堆
java·开发语言·数据结构
千寻技术帮2 小时前
10333_基于SpringBoot的家电进存销系统
java·spring boot·后端·源码·项目·家电进存销
dear_bi_MyOnly2 小时前
【多线程——线程状态与安全】
java·开发语言·数据结构·后端·中间件·java-ee·intellij-idea