java中Map的key包含下划線轉駝峰


本章代碼是把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; }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM