Map的键key转换为全大写
public static void main(String[] args) {
ArrayList<LinkedHashMap<String, Object>> mapArr = new ArrayList<>();
LinkedHashMap<String, Object> linked = new LinkedHashMap<>();
linked.put("key1","value1");
linked.put("key2","value2");
linked.put("key3","value3");
LinkedHashMap<String, Object> linked1 = new LinkedHashMap<>();
linked1.put("keY1","va1");
linked1.put("kEy2",223);
linked1.put("KEY3",false);
mapArr.add(linked);
mapArr.add(linked1);
System.out.println();
mapArr.forEach(System.out::println);
// stream流转换
List<LinkedHashMap<String, Object>> hashMaps = mapArr.stream()
.map(map -> {
LinkedHashMap<String, Object> linkedMap = new LinkedHashMap<>();
map.forEach((key, value) -> linkedMap.put(key.toUpperCase(), value));
return linkedMap;
})
.collect(Collectors.toList());
System.out.println(hashMaps);
Map<String, Object> upperCase = transformUpperCase((Map<String, Object>) linked1);
System.out.println(upperCase);
Map<String, Object> lowerCase = transformLowerCase((Map<String, Object>) linked1);
System.out.println(lowerCase);
}
// 将map的Key全部转换为大写
public static Map<String,Object> transformUpperCase(Map<String,Object> orgMap){
HashMap<String, Object> resultMap = new HashMap<>();
if (orgMap == null || orgMap.isEmpty()){
return resultMap;
}
Set<String> keySet = orgMap.keySet();
for (String key : keySet){
String newKey = key.toUpperCase();
resultMap.put(newKey,orgMap.get(key));
}
return resultMap;
}
// 将map的Key全部转换为小写
public static Map<String,Object> transformLowerCase(Map<String,Object> orgMap){
HashMap<String, Object> resultMap = new HashMap<>();
if (orgMap == null || orgMap.isEmpty()){
return resultMap;
}
Set<String> keySet = orgMap.keySet();
for (String key : keySet){
String newKey = key.toLowerCase();
resultMap.put(newKey,orgMap.get(key));
}
return resultMap;
}
本文来自博客园,作者:ElloeStudy,转载请注明原文链接:https://www.cnblogs.com/ElloeStudy/p/17651542.html