你是否必须使用HashMap?如果只需要Map Interface,请使用TreeMap
如果要通过比较HashMap中的值进行排序。你必须编写代码才能执行此操作,如果要执行此操作,则可以对HashMap的值进行排序:
Map<String, Person> people = new HashMap<>();Person jim = new Person("Jim", 25);Person scott = new Person("Scott", 28);Person anna = new Person("Anna", 23);people.put(jim.getName(), jim);people.put(scott.getName(), scott);people.put(anna.getName(), anna);// not yet sortedList<Person> peopleByAge = new ArrayList<>(people.values());Collections.sort(peopleByAge, Comparator.comparing(Person::getAge));for (Person p : peopleByAge) { System.out.println(p.getName() + "t" + p.getAge());}如果你想经常访问此排序列表,则可以将元素插入到中
HashMap<TreeSet<Person>>,尽管集合和列表的语义有些不同。



