按字母顺序按值排序HashMap [英] Order HashMap alphabetically by value

查看:86
本文介绍了按字母顺序按值排序HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 HashMap< Object,Student> 其中对象是学生的ID,学生是学生的对象。



如何使用学生姓名 student-> getName()

$ b



相反,你可以使用

= http://download.oracle.com/javase/6/docs/api/java/util/SortedMap.html\"> SortedMap 实现,例如 TreeMap

但是,即使排序的地图只能按其键排序。



如果您想按值排序,您需要将它们复制到排序列表。


I have a HashMap<Object, Student> where the Object is the ID of the Student, and the Student is an object from Student.

How can I resort the HashMap by the Students name, student->getName()?

解决方案

HashMaps are intrinsically unordered and cannot be sorted.

Instead, you can use a SortedMap implementation, such as a TreeMap.
However, even a sorted map can only sort by its keys.

If you want to sort by the values, you'll need to copy them to a sorted list.

这篇关于按字母顺序按值排序HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆