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

效果示例 :

相关推荐
U盘失踪了44 分钟前
Java 的 JAR 是什么?
java·jar
今天又在写代码1 小时前
java-v2
java·开发语言
competes2 小时前
慈善基金投资底层逻辑应用 顶层代码低代码配置平台开发结构方式数据存储模块
java·开发语言·数据库·windows·sql
2501_913061343 小时前
网络原理知识
java·网络
希望永不加班3 小时前
Spring AOP 代理模式:CGLIB 与 JDK 动态代理区别
java·开发语言·后端·spring·代理模式
flushmeteor3 小时前
java的动态代理和字节码生成技术
java·动态代理·代理·字节码生成
eggwyw3 小时前
基于SpringBoot和PostGIS的云南与缅甸的千里边境线实战
java·spring boot·spring
0xDevNull4 小时前
MySQL 别名(Alias)指南:从入门到避坑
java·数据库·sql
lv__pf4 小时前
springboot原理
java·spring boot·后端
java1234_小锋4 小时前
Java高频面试题:什么是可重入锁?
java·开发语言