MapUtil

阅读更多
做项目的感想:
     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
     * @功能描述 
取出mapkey对应的value值并转换为String
* @param map * @param key * @return */ public static String getString(final Map map, final Object key) { return getStringDefault(map,key,null); } /** * @方法名称 getStringDefault * @功能描述
取出mapkey对应的value值并转换为String,否则返回默认值
* @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 * @功能描述
取出mapkey对应的value值并转换为Integer
* @param map * @param key * @return */ public static Integer getInteger(final Map map, final Object key) { return MapUtils.getInteger(map, key); } /** * @方法名称 getLong * @功能描述
取出mapkey对应的value值并转换为Long
* @param map * @param key * @return */ public static Long getLong(final Map map, final Object key) { return MapUtils.getLong(map, key); } /** * @方法名称 getNumber * @功能描述
取出mapkey对应的value值并转换为Number
* @param map * @param key * @return */ public static Number getNumber(final Map map, final Object key) { return MapUtils.getNumber(map, key); } /** * @方法名称 getDate * @功能描述
取出mapkey对应的value值并转换为date
* @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 * @功能描述
取出mapkey对应的value值并转换为BigDecimal
* @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 * @功能描述
取出mapkey对应的value值并转换为Boolean
* @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 object2Map(Object object){ JSONObject jsonObject = (JSONObject) JSON.toJSON(object); Set> entrySet = jsonObject.entrySet(); Map map=new HashMap(); for (Map.Entry entry : entrySet) { map.put(entry.getKey(), entry.getValue()==null?"":entry.getValue()); } return map; } }

 

你可能感兴趣的:(MapUtil)