import java.util.*;
public class MapKeySort {
public static void main(String[] args) {
Map<String, String> map = new LinkedHashMap<>();
map.put("Wang", "B");
map.put("Zhang", "A");
map.put("Li", "C");
map.put("1", "B");
map.put("3", "A");
map.put("2", "C");
map = MapKeySort.sortMapByKey(map);
// System.out.println(map);
for (Map.Entry<String, String> mapEntry : map.entrySet()) {
System.out.println(mapEntry);
}
}
// 新的map(新的map需为LinkedHashMap,可按照put的顺序get)按排序后的key进行put
public static Map<String, String> sortMapByKey(Map<String, String> map) {
Set<String> mapKeySet = map.keySet();
// 等效写法一
// Set<String> newMapKeySet = new TreeSet<>(new Comparator<String>(){
// @Override
// public int compare(String o1, String o2) {
// return o1.compareTo(o2);
// }
// });
// 等效写法二
// Set<String> newMapKeySet = new TreeSet<>((o1, o2) -> o1.compareTo(o2));
// 等效写法三
// Set<String> newMapKeySet = new TreeSet<>(String::compareTo);
// 等效写法四,Comparator.naturalOrder()为升序
// Set<String> newMapKeySet = new TreeSet<>(Comparator.naturalOrder());
// newMapKeySet.addAll(mapKeySet);
// 等效写法五,默认为字符升序排序,非默认则采用其他写法
Set<String> newMapKeySet = new TreeSet<>(mapKeySet);
// 只适用于数字
// newMapKeySet.stream().sorted(Comparator.naturalOrder());
Map<String, String> newMap = new LinkedHashMap<>();
for (String key : newMapKeySet) {
newMap.put(key, map.get(key));
}
return newMap;
}
// key降序
public static <K extends Comparable<? super K>, V> Map<K, V> sortMapByKeyWithDescend(Map<K, V> map) {
List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, (o1, o2) -> {
int compare = (o1.getKey()).compareTo(o2.getKey());
return -compare;
});
Map<K, V> result = new LinkedHashMap<>();
for (Map.Entry<K, V> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}
// key升序
public static <K extends Comparable<? super K>, V> Map<K, V> sortMapByKeyWithAscend(Map<K, V> map) {
List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, (o1, o2) -> {
int compare = (o1.getKey()).compareTo(o2.getKey());
return compare;
});
Map<K, V> result = new LinkedHashMap<>();
for (Map.Entry<K, V> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}
// value降序
public static <K, V extends Comparable<? super V>> Map<K, V> sortMapByValueWithDescend(Map<K, V> map) {
List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, (o1, o2) -> {
int compare = (o1.getValue()).compareTo(o2.getValue());
return -compare;
});
Map<K, V> result = new LinkedHashMap<>();
for (Map.Entry<K, V> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}
// value升序
public static <K, V extends Comparable<? super V>> Map<K, V> sortMapByValueWithAscend(Map<K, V> map) {
List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, (o1, o2) -> {
int compare = (o1.getValue()).compareTo(o2.getValue());
return compare;
});
Map<K, V> result = new LinkedHashMap<>();
for (Map.Entry<K, V> entry : list) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}
// 将Map的entrySet按key排序,只适用于数字
// 实验失败,newMapEntrySet.stream().sorted(Comparator.comparing(Map.Entry::getKey));效果达不到预期,建议官方完善
/*public static Map<String, String> sortMapByKey(Map<String, String> map){
// 克隆,覆盖原有
// map = new LinkedHashMap<>(map);
// 克隆,不改变原有
Map<String, String> newMap = new LinkedHashMap<>(map);
Set<Map.Entry<String, String>> newMapEntrySet = newMap.entrySet();
// 等效方法一,只适用于数字
// newMapEntrySet.stream().sorted((o1, o2) -> o1.getKey().compareTo(o2.getKey()));
// 等效方法二,只适用于数字
newMapEntrySet.stream().sorted(Comparator.comparing(Map.Entry::getKey));
// 不可行,因为Map.Entry未继承Comparable接口
// Set<Map.Entry<String, String>> newMapEntrySet1 = new TreeSet<>(newMapEntrySet);
// 不可行,因为Map.Entry未继承Comparable接口
// newMapEntrySet1.addAll(newMapEntrySet);
return newMap;
}*/
}