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
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM