---
title: "MapUtil"
author: "gwzan"
date: "2019-05-27T19:26:34Z"
tags: []
categories: ["编程语言"]
slug: "maputil"
draft: false
lastmod: "2019-05-27T19:39:17Z"
id: 2438698
url: "https://gwzan.iteye.com/blog/2438698"
---

```
做项目的感想：
     1.尽量用项目中已经封装好的工具类，切忌自己又去写（减少垃圾代码），使代码更规范
     2.了解性能及原理，用好API，就是在减少bug的出现，避免踩坑

package com.pingan.scf.core.server.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.MapUtils;

import java.math.BigDecimal;
import java.util.*;

/**
 * 参数Map转换类
*/
public class MapUtil {

    /**
     * @方法名称 getString
     * @功能描述 <pre>取出map中key对应的value值并转换为String型</pre>     * @param map
* @param key
* @return
*/
public static String getString(final Map map, final Object key) {
        return getStringDefault(map,key,null);
    }
    /**
     * @方法名称 getStringDefault
     * @功能描述 <pre>取出map中key对应的value值并转换为String型,否则返回默认值</pre>* @param key
* @return
*/
public static String getStringDefault(final Map map, final Object key,String value) {
        return MapUtils.getString(map, key)==null? value:MapUtils.getString(map, key);
    }

    /**
     *
     * @方法名称 getInteger
     * @功能描述 <pre>取出map中key对应的value值并转换为Integer型</pre>     * @param map
* @param key
* @return
*/
public static Integer getInteger(final Map map, final Object key) {
        return MapUtils.getInteger(map, key);
    }

    /**
     * @方法名称 getLong
     * @功能描述 <pre>取出map中key对应的value值并转换为Long型</pre>     * @param map
* @param key
* @return
*/
public static Long getLong(final Map map, final Object key) {
        return MapUtils.getLong(map, key);
    }

    /**
     * @方法名称 getNumber
     * @功能描述 <pre>取出map中key对应的value值并转换为Number型</pre>     * @param map
* @param key
* @return
*/
public static Number getNumber(final Map map, final Object key) {
        return MapUtils.getNumber(map, key);
    }

    /**
     * @方法名称 getDate
     * @功能描述 <pre>取出map中key对应的value值并转换为date型</pre>     * @param map
* @param key
* @return
*/
public static Date getDate(final Map map, final Object key) {
        if (map != null){
            Object answer = map.get(key);
            if (answer != null) {
                if (answer instanceof Date) {
                    return (Date) answer;
                }
            }
        }
        return null;
    }

    /**
     * @方法名称 getBigDecimal
     * @功能描述 <pre>取出map中key对应的value值并转换为BigDecimal型</pre>     * @param map
* @param key
* @return
*/
public static BigDecimal getBigDecimal(final Map map, final Object key) {
        if (map != null){
            Object answer = map.get(key);
            if (answer != null) {
                if (answer instanceof BigDecimal) {
                    return (BigDecimal) answer;
                }
                else if(answer instanceof String) {
                    return new BigDecimal((String)answer);
                }else if(answer instanceof Integer) {
                    return new BigDecimal((Integer)answer);
                }else if(answer instanceof Long) {
                    return new BigDecimal((Long)answer);
                }else if(answer instanceof Double) {
                    return new BigDecimal((Double)answer);
                }else if(answer instanceof Float) {
                    return new BigDecimal((Float)answer);
                }
            }
        }
        return null;
    }

    /**
     * @方法名称 getBigDecimal
     * @功能描述 <pre>取出map中key对应的value值并转换为Boolean型</pre>     * @param map
* @param key
* @return
*/
public static Boolean getBoolean(final Map map, final Object key) {
        if (map != null) {
            Object answer = map.get(key);
            if (answer != null) {
                if (answer instanceof Boolean) {
                    return (Boolean) answer;

                } else if (answer instanceof String) {
                    return new Boolean((String) answer);

                } else if (answer instanceof Number) {
                    Number n = (Number) answer;
                    return (n.intValue() != 0) ? Boolean.TRUE : Boolean.FALSE;
                }
            }
        }
        return false;
    }

    public static List getList(final Map map, final Object key){
        if (map == null)
            return null;
        if (key == null)
            return null;
        if (!map.containsKey(key)) {
            return null;
        }
        Object value = map.get(key);
        if (value == null)
            return null;

        if (List.class.isAssignableFrom(value.getClass())) {
            return (List) value;
        }
        return null;
    }

    /**
     * 对象转map
     * @param object
* @return
*/
public static Map<String, Object> object2Map(Object object){
        JSONObject jsonObject = (JSONObject) JSON.toJSON(object);
        Set<Map.Entry<String,Object>> entrySet = jsonObject.entrySet();
        Map<String, Object> map=new HashMap<String,Object>();
        for (Map.Entry<String, Object> entry : entrySet) {
            map.put(entry.getKey(), entry.getValue()==null?"":entry.getValue());
        }
        return map;
    }
}
```