1、Object转Map
2、将 map 中的 value 为null的的元素转成空字符串""
3、map转Object
4、MAP 判空
5、Map 排序 按key值长度进行排序
6、两个MAP对比,在oldMap中移除remMap中所有的key值
备注:排序的话(TreeMap)是可以直接实现简单排序的,如果有复杂的业务场景可以重写Comparator
引入jar
<dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.3</version> </dependency>
import org.apache.commons.beanutils.BeanMap; import org.apache.commons.beanutils.BeanUtils; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util.*; import static org.apache.commons.beanutils.BeanUtils.populate; /** * MAP工具类 * TreeMap是有序的 默认正序排列 */ public class MapUtils { /** * Object转Map * * @param obj * @return */ public static Map<?, ?> objectToMap(Object obj) { if (obj == null) { return null; } return new BeanMap(obj); } /** * Object转Map * * @param obj * @return * @throws IllegalAccessException */ public static Map<?, ?> objectToMapA(Object obj) throws IllegalAccessException { if (obj == null) { return null; } Map<String, Object> map = new HashMap<>(); Class<?> clazz = obj.getClass(); for (Field field : clazz.getDeclaredFields()) { field.setAccessible(true); String fieldName = field.getName(); Object value = field.get(obj); map.put(fieldName, value); } return map; } /** * Object 转MAP * * @param obj * @return * @throws IllegalAccessException * @throws NoSuchMethodException * @throws InvocationTargetException */ public static Map<?, ?> objectToMapB(Object obj) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { if (obj == null) { return null; } //使用BeanUtils获取对象属性 return BeanUtils.describe(obj); } /** * 将 map 中的 value 为null的的元素转成空字符串"" * * @param map * @return */ public static Map<Integer, String> valueConvertString(Map<Integer, String> map) { map.forEach((k, v) -> { if (null == v || "".equals(v)) { map.put(k, "\" \""); } }); return map; } /** * map转Object * * @param map * @param beanClass * @return * @throws Exception */ public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) { if (map == null) { return null; } try { Object obj = beanClass.newInstance(); populate(obj, map); return obj; } catch (Exception e) { e.printStackTrace(); } return null; } /** * MAP 判空 * * @param map * @return */ public static boolean isEmpty(Map<?, ?> map) { return map == null || map.isEmpty(); } /** * Map 排序 按key值长度进行排序 * * @param map * @return */ public static Map<String, String> sortMapByKey(Map<String, String> map) { if (CommonFunctions.isEmpty(map)) { return null; } Map<String, String> sortMap = new TreeMap<>(new MapKeyComparator()); sortMap.putAll(map); return sortMap; } /** * 两个MAP对比,在oldMap中移除remMap中所有的key值 * * @param remMap 移除的key * @param oldMap 需要移除key值的MAP */ public static void removeKey(Map remMap, Map oldMap) { if (CommonFunctions.isEmpty(remMap) || CommonFunctions.isEmpty(remMap)) { return; } Iterator iterator = remMap.keySet().iterator(); while (iterator.hasNext()) { String key = (String) iterator.next(); oldMap.remove(key); } } public static void main(String[] args) { //只是简单的排序 这里足够用了 Map<String, Object> map = new TreeMap<>();//正序 map.put("A1", 3); map.put("D3", 1); map.put("B4", 2); map.put("C2", 4); map.forEach((k, v) -> System.out.println("正序====KEY==" + k + "====VALUE====" + v)); Map<String, Object> mapDesc = new TreeMap<>(new MapKeyComparatorDesc());//倒序 mapDesc.put("A1", 3); mapDesc.put("D3", 1); mapDesc.put("B4", 2); mapDesc.put("C2", 4); mapDesc.forEach((k, v) -> System.out.println("倒序====KEY==" + k + "====VALUE====" + v)); Map<String, String> keyMap = new HashMap<>(); keyMap.put("A", "Q"); keyMap.put("AA", "Q"); keyMap.put("AAAA", "Q"); keyMap.put("AAA", "Q"); keyMap.put("AAAAA", "Q"); keyMap.forEach((k, v) -> System.out.println("排序前====KEY==" + k + "====VALUE====" + v)); Map<String, String> map1 = sortMapByKey(keyMap); map1.forEach((k, v) -> System.out.println("排序后====KEY==" + k + "====VALUE====" + v)); } } //比较1 (默认比较级倒转) class MapKeyComparatorDesc implements Comparator { @Override public int compare(Object o1, Object o2) { String i1 = (String) o1; String i2 = (String) o2; return -i1.compareTo(i2); } } //比较2 (key值长度比较) class MapKeyComparator implements Comparator<String> { @Override public int compare(String o1, String o2) { if (o1.length() > o2.length()) { return -1; } else if (o1.length() < o2.length()) { return 1; } else { return o2.compareTo(o1); } } }