将map中的key全部转化为大写或者小写

package com.alphajuns.mapTest;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName MapTest
 * @Description map键转换
 * @Author AlphaJunS
 * @Date 2020/11/11 21:24
 * @Version 1.0
 */
public class MapTest {

    public static void main(String[] args) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("NAME", "张三");
        map.put("AGE", "24");
        map.put("ADDRESS", "广州");
        Map<String, String> lowerKeyMap = toLowerKey(map);
        System.out.println(lowerKeyMap);
        Map<String, String> map1 = new HashMap<String, String>();
        map1.put("name", "李四");
        map1.put("age", "25");
        map1.put("address", "深圳");
        Map<String, String> upperKeyMap = toUpperKey(map1);
        System.out.println(upperKeyMap);
    }

    /**
     * @Author AlphaJunS
     * @Date 21:50 2020/11/11
     * @Description 将map中的所有key转化为小写
     * @param map
     * @return java.util.Map<java.lang.String,java.lang.String>
     */
    public static Map<String, String> toLowerKey(Map<String, String> map) {
        Map<String, String> resultMap = new HashMap<String, String>();
        Set<String> sets = map.keySet();
        for (String key : sets) {
            resultMap.put(key.toLowerCase(), map.get(key));
        }
        return resultMap;
    }

    /**
     * @Author AlphaJunS
     * @Date 21:51 2020/11/11
     * @Description 将map中的所有key转化为大写
     * @param map
     * @return java.util.Map<java.lang.String,java.lang.String>
     */
    public static Map<String, String> toUpperKey(Map<String, String> map) {
        Map<String, String> resultMap = new HashMap<String, String>();
        Set<String> sets = map.keySet();
        for (String key : sets) {
            resultMap.put(key.toUpperCase(), map.get(key));
        }
        return resultMap;
    }

}

 

posted @ 2020-11-11 21:54  AlphaJunS  阅读(7715)  评论(0编辑  收藏  举报