根据key获取map中的value值,如果该map为null或者为空,或者找不到传入key,或者类型不一致则返回空。

//根据key获取map中的value值,如果该map为null或者为空,或者找不到传入key,或者类型不一致则返回空。
public class MapUtil {
public String getMapValue(Map<String, Object> mapPara, String strKey) {
return getMapValue(mapPara, strKey);
}
public static String getMapValue(Map<String, Object> mapPara, String strKey) {
return getMapValue(mapPara, strKey, "");
}
public static String getMapValue(Map<String, Object> mapPara, String strKey, String strDefault) {
return getMapValue(mapPara, strKey, strDefault, "String");
}
/**
* 获取Map数据中的数值
* @param mapPara 存储容器
* @param strKey 存取KEY
* @param strDefault 非法返回默认值
* @return
*/
public static String getMapValue(Map<String, Object> mapPara, String strKey, String strDefault, String strType) {
String strRes = "";

if (mapPara == null || mapPara.size() == 0) {
return strDefault;
}

if (mapPara.get(strKey) == null) {
return strDefault;
}

if (strType.equals("int")) {
strRes = (Integer) mapPara.get(strKey) + "";
}else{
strRes = (String) mapPara.get(strKey);
}

if (strRes == null || strRes.trim().equals("")) {
return strDefault;
}

strRes = strRes.trim();
return strRes;
}
}
//在使用时直接调用String wjbh = getMapValue(getParaMap(), key值);
Map<String, Object> mapPara = new hashMap<String,Object>();
String wjbh = MapUtil.getMapValue(mapPara, key值);

猜你喜欢

转载自blog.csdn.net/wzm1994/article/details/73571861