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

 

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

    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;
}

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

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值