MapUtils常用方法

 
maven引入
<dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-collections4</artifactId>
      <version>4.2</version>
    </dependency>
 
方法
可以从指定map中获取常用基础类型的值,都会判断map,map为null返回null,get结果为null返回null:
 
修饰符和返回类型    方法    描述
static <K> Boolean    getBoolean(Map<? super K,?> map, K key)    从Map获取Boolean
static <K> Boolean    getBoolean(Map<? super K,?> map, K key, Boolean defaultValue)    将结果转换为Boolean,如果转换失败则使用默认值
static <K> boolean    getBooleanValue(Map<? super K,?> map, K key)    从Map获取boolean。
static <K> boolean    getBooleanValue(Map<? super K,?> map, K key, boolean defaultValue)    如果转换失败,则使用默认值
static <K> Double    getDouble(Map<? super K,?> map, K key)    从Map获取Double
static <K> Double    getDouble(Map<? super K,?> map, K key, Double defaultValue)    将结果转换为Double,如果转换失败则使用默认值
static <K> double    getDoubleValue(Map<? super K,?> map, K key)    从Map获取double
static <K> double    getDoubleValue(Map<? super K,?> map, K key, double defaultValue)    如果转换失败,则使用默认值
static <K> Float    getFloat(Map<? super K,?> map, K key)    从Map获取Float
static <K> Float    getFloat(Map<? super K,?> map, K key, Float defaultValue)    将结果转换为Float,如果转换失败则使用默认值
static <K> float    getFloatValue(Map<? super K,?> map, K key)    从Map获取float
static <K> float    getFloatValue(Map<? super K,?> map, K key, float defaultValue)    如果转换失败,则使用默认值
static <K> Integer    getInteger(Map<? super K,?> map, K key)    从Map获取Integer
static <K> Integer    getInteger(Map<? super K,?> map, K key, Integer defaultValue)    将结果转换为Integer,如果转换失败则使用默认值
static <K> int    getIntegerValue(Map<? super K,?> map, K key)    从Map获取int
static <K> int    getIntegerValue(Map<? super K,?> map, K key, int defaultValue)    如果转换失败,则使用默认值
static <K> Long    getLong(Map<? super K,?> map, K key)    从Map获取Long
static <K> Long    getLong(Map<? super K,?> map, K key, Long defaultValue)    将结果转换为Long,如果转换失败则使用默认值
static <K> long    getLongValue(Map<? super K,?> map, K key)    从Map获取long
static <K> long    getLongValue(Map<? super K,?> map, K key, long defaultValue)    如果转换失败,则使用默认值
static <K> String    getString(Map<? super K,?> map, K key)    从Map获取String
static <K> String    getString(Map<? super K,?> map, K key, String defaultValue)    将结果转换为String,如果转换失败则使用默认值
此外还可以获取Number、Short、Byte等类型值
 
实列
Map<String, Object> nullMap = null;
Map<String, Object> map = new HashMap<>();
map.put("user", new User());
 
map.put("boolean", true);
Assert.assertTrue(MapUtils.getBoolean(map, "boolean"));
//转换失败,返回默认值false
Assert.assertFalse(MapUtils.getBoolean(map, "user",false));
//目标map为null,返回null
Assert.assertNull(MapUtils.getBoolean(nullMap, "boolean"));
//目标map为null,返回默认值false
Assert.assertFalse(MapUtils.getBoolean(nullMap, "boolean", false));
 
Assert.assertTrue(MapUtils.getBooleanValue(map, "boolean"));
//转换失败,返回默认值false
Assert.assertFalse(MapUtils.getBooleanValue(map, "user",false));
//目标map为null,返回false
Assert.assertFalse(MapUtils.getBooleanValue(nullMap, "boolean"));
//目标map为null,返回默认值false
Assert.assertFalse(MapUtils.getBooleanValue(nullMap, "boolean", false));
 
map.put("integer", 5);
Assert.assertEquals(Integer.valueOf(5), MapUtils.getInteger(map, "integer"));
//转换失败,返回默认值5
Assert.assertEquals(Integer.valueOf(5), MapUtils.getInteger(map, "integer",5));
//目标map为null,返回null
Assert.assertEquals(null, MapUtils.getInteger(nullMap, "integer"));
//目标map为null,返回默认值5
Assert.assertEquals(Integer.valueOf(5), MapUtils.getInteger(nullMap, "integer", 5));
 
Assert.assertEquals(5, MapUtils.getIntValue(map, "integer"));
//转换失败,返回默认值5
Assert.assertEquals(5, MapUtils.getIntValue(map, "user",5));
//目标map为null,返回0
Assert.assertEquals(0, MapUtils.getIntValue(nullMap, "integer"));
//目标map为null,返回默认值5
Assert.assertEquals(5, MapUtils.getIntValue(nullMap, "integer", 5));
---------------------
作者:借物小人
来源:CSDN
版权声明:本文为博主原创文章,转载请附上博文链接!
原文地址:https://www.cnblogs.com/TJGKK/p/11282866.html