public class SortedMap { //treemap按key排序,默认是升序,可自定义降序 public static void main(String[] args) { Map<Integer, String> map = new TreeMap<>(new Comparator<Integer>() { public int compare(Integer a, Integer b) { //return a < b ? 1 : (a == b)? 0 : -1; return b.compareTo(a); } } ); map.put(5, "java"); map.put(2, "c"); map.put(6, "jsp"); System.out.println(map); } }