java Map常用方法封装
java Map常用方法封装
CreationTime--2018年7月16日15点59分
Author:Marydon
1.准备工作
import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.SortedMap; import java.util.TreeMap; import org.apache.log4j.Logger; /** * Map数据处理工具类 * @explain * @author Marydon * @creationTime 2018年7月9日上午9:17:37 * @version 1.0 * @since * @email marydon20170307@163.com */ public class MapUtils { private static Logger log = Logger.getLogger(MapUtils.class); }
2.无序Map转换成有序Map
/** * 将无序Map转换成有序Map * @explain 将参数按照第一个字符的键值 ASCII 码递增排序(字母升序排序),如果遇到相同字符则按照第二个字符的键值 ASCII 码递增排序, * 以此类推通过SortedMap可以实现 * @param paramsMap * @return Map 按ASCII码升序排列 */ public static Map<String, Object> toSortedMap(Map<String, Object> paramsMap) { // 用于存储接收到的key:value,并按key以ASCII码进行升序排列组合 SortedMap<String, Object> data = new TreeMap<String, Object>(); // 获取Map对象中的键值对对象 Set<Entry<String, Object>> entrySet = paramsMap.entrySet(); Entry<String, Object> entry = null; String key = ""; Object value = null; // 遍历paramsMap数据,添加到SortedMap对象 for (Iterator<Entry<String, Object>> iterator = entrySet.iterator(); iterator.hasNext();) { entry = iterator.next(); key = entry.getKey(); value = entry.getValue(); data.put(key, value); } log.info("Map-->SortedMap:" + data); return data; }
3.将map的value转化成String类型
/** * Map<String, Object>-->Map<String, String> * @explain 前提:value必须为String类型 * @param paramsMap * @return * @throws Exception */ public static Map<String, String> toStringMap(Map<String, Object> paramsMap) throws Exception{ Map<String, String> data = new HashMap<String, String>(); // 获取Map对象中的键值对对象 Set<Entry<String, Object>> entrySet = paramsMap.entrySet(); Entry<String, Object> entry = null; String key = ""; String value = null; for (Iterator<Entry<String, Object>> iterator = entrySet.iterator(); iterator.hasNext();) { entry = iterator.next(); key = entry.getKey(); if (key instanceof String) { value = (String) entry.getValue(); data.put(key, value); } else { log.error("Map中含有非String类型的值,对应的key=" + key); throw new Exception("Map中含有非String类型的值,对应的key=" + key); } } log.info("Map<String, Object>-->Map<String, String>:" + data); return data; }
4.将map的value转化成Object类型
/** * Map<String, String>-->Map<String, Object> * @explain 将value转换成Object类型 * @param paramsMap * @return */ public static Map<String, Object> toObjectMap(Map<String, String> paramsMap){ Map<String, Object> data = new HashMap<String, Object>(); // 获取Map对象中的键值对对象 Set<Entry<String, String>> entrySet = paramsMap.entrySet(); Entry<String, String> entry = null; String key = ""; Object value = null; for (Iterator<Entry<String, String>> iterator = entrySet.iterator(); iterator.hasNext();) { entry = iterator.next(); key = entry.getKey(); value = entry.getValue(); data.put(key, value); } log.info("Map<String, String>-->Map<String, Object>:" + data); return data; }
5.将map的key小写转换成大写
/** * key 小写-->大写 * @explain 将key改为大写 * @param paramsMap * @return */ public static Map<String, Object> toUpperMap(Map<String, Object> paramsMap){ Map<String, Object> data = new HashMap<String, Object>(); // 获取Map对象中的键值对对象 Set<Entry<String, Object>> entrySet = paramsMap.entrySet(); Entry<String, Object> entry = null; String key = ""; Object value = null; for (Iterator<Entry<String, Object>> iterator = entrySet.iterator(); iterator.hasNext();) { entry = iterator.next(); key = entry.getKey().toUpperCase(); value = entry.getValue(); data.put(key, value); } log.info("Map 小写-->大写:" + data); return data; }
本文来自博客园,作者:Marydon,转载请注明原文链接:https://www.cnblogs.com/Marydon20170307/p/9318179.html