1.背景
从Map中获取指定类型的值,获取不到则采用默认值,本身功能不是很复杂,也可以自己定义,工具包org.apache.commons.collections.MapUtils提供的有,使用起来也比较方便,本篇文章要验证的是当MapUtils.getIntValue中取int类型或者非int类型值的时候分别获取默认值还是转换后的值?
2.案例验证
/**
* 工具包
* 从Map中获取指定类型的值,获取不到则采用默认值
* import org.apache.commons.collections.MapUtils;
* MapUtils.getIntValue中获取值
* int类型获取int类型值
* String类型分为两种,可转换为int的取int值,不可以转换int的取默认值
* double类型的转换int后,取int类型值
* 其他非int类型值,比如null,""等取默认值
*/
@Test
public void MapUtilsGetValueTest() {
System.out.println("-------------MapUtilsTest-------------");
Map<String, Object> queryCrowdMap = new HashMap<>();
queryCrowdMap.put("intTest", 6);
queryCrowdMap.put("strTest1", "strTest");
queryCrowdMap.put("strTest2", "666");
queryCrowdMap.put("doubleTest", 6.6);
queryCrowdMap.put("nullTest", null);
queryCrowdMap.put("emptyTest", "");
System.out.println("int测试");
int intTest = MapUtils.getIntValue(queryCrowdMap, "intTest", 1);
System.out.println("intTest=" + intTest);// 6
System.out.println("非int测试");
// 如果MapUtils.getIntValue取的是String类型,且string类型不可以转换int,则获取默认值
int strTest1 = MapUtils.getIntValue(queryCrowdMap, "strTest1", 1);
System.out.println("strTest1=" + strTest1);// 1
// 如果MapUtils.getIntValue取的是String类型,且string类型可以转换int,则转换后去int类型值
int strTest2 = MapUtils.getIntValue(queryCrowdMap, "strTest2", 1);
System.out.println("strTest2=" + strTest2);// 666
// 如果MapUtils.getIntValue取的是double类型,则获取double转int类型
int doubleTest = MapUtils.getIntValue(queryCrowdMap, "doubleTest", 1);
System.out.println("doubleTest=" + doubleTest);// 6
// 如果MapUtils.getIntValue取的是null类型,则获取默认值
int nullTest = MapUtils.getIntValue(queryCrowdMap, "nullTest", 1);
System.out.println("nullTest=" + nullTest);// 1
// 如果MapUtils.getIntValue取的是String类型空字符串,则获取默认值
int emptyTest = MapUtils.getIntValue(queryCrowdMap, "emptyTest", 1);
System.out.println("emptyTest=" + emptyTest);// 1
}
3.验证结论
工具包, 从Map中获取指定类型的值,获取不到则采用默认值
import org.apache.commons.collections.MapUtils;
MapUtils.getIntValue中获取值
int类型获取int类型值
String类型分为两种,可转换为int的取int值,不可以转换int的取默认值
double类型的转换int后,取int类型值
其他非int类型值,比如null,""等取默认值
4.源码分析
//测试类
// 如果MapUtils.getIntValue取的是String类型,则获取默认值
int strTest = MapUtils.getIntValue(queryCrowdMap, "strTest", 1);
// org.apache.commons.collections.MapUtils
public static int getIntValue(final Map map, final Object key, int defaultValue) {
// 获取转换integer
Integer integerObject = getInteger(map, key);
if (integerObject == null) {
return defaultValue;
}
return integerObject.intValue();
}
// org.apache.commons.collections.MapUtils
public static Integer getInteger(final Map map, final Object key) {
//转换获取Number类型
Number answer = getNumber(map, key);
if (answer == null) {
return null;
} else if (answer instanceof Integer) {
return (Integer) answer;
}
return new Integer(answer.intValue());
}
//org.apache.commons.collections.MapUtils
public static Number getNumber(final Map map, final Object key) {
if (map != null) {
Object answer = map.get(key);
if (answer != null) {
if (answer instanceof Number) {
return (Number) answer;
} else if (answer instanceof String) {
try {
String text = (String) answer;
// 为string类型的时候处理逻辑,这里获取null
// String类型分为两种,可转换为int的取int值,不可以转换int的取默认值
return NumberFormat.getInstance().parse(text);
} catch (ParseException e) {
//打印类型转换异常日志
logInfo(e);
}
}
}
}
//其他类型返回null
return null;
}
//java.text.NumberFormat
public Number parse(String source) throws ParseException {
ParsePosition parsePosition = new ParsePosition(0);
//string类型的是这里解析返回null
//String类型分为两种,可转换为int的取int值,不可以转换int的取默认值
Number result = parse(source, parsePosition);
if (parsePosition.index == 0) {
throw new ParseException("Unparseable number: \"" + source + "\"",
parsePosition.errorIndex);
}
return result;
}