Java教程

java中Map的key包含下划线转驼峰

本文主要是介绍java中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;
    }

 

这篇关于java中Map的key包含下划线转驼峰的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!