MapUtils常用方法

包路径:org.apache.commons.collections4.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> BooleangetBoolean(Map<? super K,?> map, K key)从Map获取Boolean
static <K> BooleangetBoolean(Map<? super K,?> map, K key, Boolean defaultValue)将结果转换为Boolean,如果转换失败则使用默认值
static <K> booleangetBooleanValue(Map<? super K,?> map, K key)从Map获取boolean。
static <K> booleangetBooleanValue(Map<? super K,?> map, K key, boolean defaultValue)如果转换失败,则使用默认值
static <K> DoublegetDouble(Map<? super K,?> map, K key)从Map获取Double
static <K> DoublegetDouble(Map<? super K,?> map, K key, Double defaultValue)将结果转换为Double,如果转换失败则使用默认值
static <K> doublegetDoubleValue(Map<? super K,?> map, K key)从Map获取double
static <K> doublegetDoubleValue(Map<? super K,?> map, K key, double defaultValue)如果转换失败,则使用默认值
static <K> FloatgetFloat(Map<? super K,?> map, K key)从Map获取Float
static <K> FloatgetFloat(Map<? super K,?> map, K key, Float defaultValue)将结果转换为Float,如果转换失败则使用默认值
static <K> floatgetFloatValue(Map<? super K,?> map, K key)从Map获取float
static <K> floatgetFloatValue(Map<? super K,?> map, K key, float defaultValue)如果转换失败,则使用默认值
static <K> IntegergetInteger(Map<? super K,?> map, K key)从Map获取Integer
static <K> IntegergetInteger(Map<? super K,?> map, K key, Integer defaultValue)将结果转换为Integer,如果转换失败则使用默认值
static <K> intgetIntegerValue(Map<? super K,?> map, K key)从Map获取int
static <K> intgetIntegerValue(Map<? super K,?> map, K key, int defaultValue)如果转换失败,则使用默认值
static <K> LonggetLong(Map<? super K,?> map, K key)从Map获取Long
static <K> LonggetLong(Map<? super K,?> map, K key, Long defaultValue)将结果转换为Long,如果转换失败则使用默认值
static <K> longgetLongValue(Map<? super K,?> map, K key)从Map获取long
static <K> longgetLongValue(Map<? super K,?> map, K key, long defaultValue)如果转换失败,则使用默认值
static <K> StringgetString(Map<? super K,?> map, K key)从Map获取String
static <K> StringgetString(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));
//目标mapnull,返回null
Assert.assertNull(MapUtils.getBoolean(nullMap, "boolean"));
//目标mapnull,返回默认值false
Assert.assertFalse(MapUtils.getBoolean(nullMap, "boolean", false));

Assert.assertTrue(MapUtils.getBooleanValue(map, "boolean"));
//转换失败,返回默认值false
Assert.assertFalse(MapUtils.getBooleanValue(map, "user",false));
//目标mapnull,返回false
Assert.assertFalse(MapUtils.getBooleanValue(nullMap, "boolean"));
//目标mapnull,返回默认值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));
//目标mapnull,返回null
Assert.assertEquals(null, MapUtils.getInteger(nullMap, "integer"));
//目标mapnull,返回默认值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));
//目标mapnull,返回0
Assert.assertEquals(0, MapUtils.getIntValue(nullMap, "integer"));
//目标mapnull,返回默认值5
Assert.assertEquals(5, MapUtils.getIntValue(nullMap, "integer", 5));
  • 6
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值