Java:org.apache.commons.collections4.MapUtils获取Map数据

简介: Java:org.apache.commons.collections4.MapUtils获取Map数据

依赖

<dependency>
  <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.4</version>
</dependency>

示例

package com.demo.map;
import org.apache.commons.collections4.MapUtils;
import java.util.HashMap;
import java.util.Map;
public class MapUtilDemo {
    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>();
        map.put("age", 12);
    // Integer age = (Integer) map.getOrDefault("age", 20);
        Integer age = MapUtils.getInteger(map, "age", 20);
        System.out.println("age: " + age);
        // age: 20
    }
}

如果是字符串类型

Map<String, Object> map = new HashMap<>();
map.put("age", "12");
// Integer age = (Integer) map.getOrDefault("age", 20);
// java.lang.String cannot be cast to java.lang.Integer
Integer age = MapUtils.getInteger(map, "age", 20);
System.out.println("age: " + age);
// age: 12
相关文章
|
6月前
|
移动开发 数据库 索引
"java.lang.ArrayIndexOutOfBoundsException: 0\r\n\tat org.neo4j.ogm.context.EntityRowModelMapper.extractColumnValue(EntityRowModelMapper.java:75) 问题处理
"java.lang.ArrayIndexOutOfBoundsException: 0\r\n\tat org.neo4j.ogm.context.EntityRowModelMapper.extractColumnValue(EntityRowModelMapper.java:75) 问题处理
58 5
org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
103 0
|
Java
SpingBoot2.x文件上传:failed to convert java.lang.String to org.springframework.util.unit.DataSize
SpingBoot2.x文件上传:failed to convert java.lang.String to org.springframework.util.unit.DataSize
415 0
|
应用服务中间件
报错java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.问题出现原因,及解决办法
报错java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.问题出现原因,及解决办法
213 0
|
Java 应用服务中间件 Apache
nested exception is java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntim
nested exception is java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntim
369 0
|
Java Apache
java.lang.NoClassDefFoundError:org/apache/commons/logging/LogFactory
java.lang.NoClassDefFoundError:org/apache/commons/logging/LogFactory
117 0
|
Java 应用服务中间件
Caused by: java.lang.ClassNotFoundException: org.apache.solr.handler.dataimportHandler
Caused by: java.lang.ClassNotFoundException: org.apache.solr.handler.dataimportHandler
305 0
java.util.NoSuchElementException: No mapping for class sun.awt.AppContext
学习java.util.NoSuchElementException: No mapping for class sun.awt.AppContext。
107 0
Java:org.apache.commons.lang3.StringUtils判断字符串为空
Java:org.apache.commons.lang3.StringUtils判断字符串为空
201 0
|
Dubbo 应用服务中间件 Apache
java.lang.NoClassDefFoundError: org/apache/curator/framework/CuratorFrameworkFactory
java.lang.NoClassDefFoundError: org/apache/curator/framework/CuratorFrameworkFactory
498 0
java.lang.NoClassDefFoundError: org/apache/curator/framework/CuratorFrameworkFactory

推荐镜像

更多