本章代碼是把Map中的key包含下划線的轉成駝峰
map不支持直接修改key,所以只能刪除在添加
直接上代碼:
public static Map<String, Object> replaceHump(Map<String, Object> oldMap) { Map<String, Object> newObjectMap = new HashMap<String, Object>(); Set<Map.Entry<String, Object>> entries = oldMap.entrySet(); Iterator<Map.Entry<String, Object>> iterator = entries.iterator(); while (iterator.hasNext()) { Map.Entry<String, Object> next = iterator.next(); String key = next.getKey(); if (StringUtils.contains(key, "_")) { Object value = oldMap.get(key); String newKey = Util.toCamelCase(key); newObjectMap.put(newKey, value); iterator.remove(); } } for (String newStrKey : newObjectMap.keySet()) { oldMap.put(newStrKey, newObjectMap.get(newStrKey)); } return newObjectMap; }