Map根据key排序

it2025-09-05  4

public static <K extends Comparable<? super K>, V > Map<K, V> sortByKey(Map<K, V> map) { Map<K, V> result = new LinkedHashMap<>(); map.entrySet().stream() .sorted(Map.Entry.<K, V>comparingByKey() ).forEachOrdered(e -> result.put(e.getKey(), e.getValue())); return result; }
最新回复(0)