直接上代码:
package com.shiqi.util;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* Map转换工具类
*
* @author shiqi
* @date 2023/11/06
*/
public class MapUtil {
public static void main(String[] args) {
//转换为小写
Map<String, Object> map = new HashMap<>();
map.put("ABC", "转换为小写1");
map.put("dEf", "转换为小写2");
map = keyToLowerCase(map);
System.out.println(map);
//转换为大写
Map<String, Object> map1 = new HashMap<>();
map1.put("ghi", "转换为大写1");
map1.put("jKl", "转换为大写2");
map1 = keyToUpperCase(map1);
System.out.println(map1);
}
/**
* map类型的数据key转换为小写
*
* @param orgMap
* @return {@link Map }<{@link String }, {@link Object }>
* @author shiqi
* @date 2023/11/06
*/
public static Map<String, Object> keyToLowerCase(Map<String, Object> orgMap) {
Map<String, Object> resultMap = new HashMap<>();
if (orgMap == null || orgMap.isEmpty()) {
return resultMap;
}
Set<Map.Entry<String, Object>> entrySet = orgMap.entrySet();
for (Map.Entry<String, Object> entry : entrySet) {
String key = entry.getKey();
Object value = entry.getValue();
resultMap.put(key.toLowerCase(), value);
}
return resultMap;
}
/**
* map类型的数据key转换为大写
*
* @param orgMap
* @return {@link Map }<{@link String }, {@link Object }>
* @author shiqi
* @date 2023/11/06
*/
public static Map<String, Object> keyToUpperCase(Map<String, Object> orgMap) {
Map<String, Object> resultMap = new HashMap<>();
if (orgMap == null || orgMap.isEmpty()) {
return resultMap;
}
Set<Map.Entry<String, Object>> entrySet = orgMap.entrySet();
for (Map.Entry<String, Object> entry : entrySet) {
String key = entry.getKey();
Object value = entry.getValue();
resultMap.put(key.toUpperCase(), value);
}
return resultMap;
}
}