根据属性而不是值对 HashMap 对象进行排序
这不是我刚刚模拟的真实代码,以便了解下一步该做什么。
我有一个具有年龄、身高、体重属性的 Person 类。
现在在我的班级小组
我创建了两个四个对象
Person programmer, student, clerk, tech;
,我有 HashMap rollCall
Map<Person, Integer> rollCall = new HashMap<Person, Integer>();
使用 Person 和人员数量作为 Integer 类型来添加所有这些对象
rollCall.put(programmer, 1);
rollCall.put(clerk, 2);
rollCall.put(student, 1);
rollCall.put(tech, 3);
我看到很多人使用 TreeMap 对 HashMap 进行排序 我想对 Person 的属性进行排序关于价值。我想按年龄对所有这些人进行排序(即programmer.getAge();)。我不确定是否会使用仅适用于集合而非地图的比较器。 。 请帮忙... 。
This is not my real code I have just simulated in order to understand what to do next.
I have class Person with properties age, height weight.
Now In my class Group
I create two four objects
Person programmer, student, clerk, tech;
I have HashMap rollCall
Map<Person, Integer> rollCall = new HashMap<Person, Integer>();
to add all these using Person and number of Persons as type Integer
rollCall.put(programmer, 1);
rollCall.put(clerk, 2);
rollCall.put(student, 1);
rollCall.put(tech, 3);
I have seen alot of people sorting HashMap using TreeMap on value I want to sort on a property of Person rather on value. I want to sort all these people on their age (i.e. programmer.getAge();). I am not sure if I will use comprator which works only on collection not map.
.
Please help ...
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以获得一个
Map
,它通过使用自定义比较器按年龄递增或递减顺序进行迭代:当您将 Persons 添加到集合中时,它们将按年龄顺序插入。
You can get a
Map<Person,Integer>
which iterates by age increasing or decreasing order by using a custom comparator:When you add Persons to the collection they will be inserted in order by age.
您需要能够比较您的 Person 对象。如果有规范的方法来比较它们,请让它们实现 Comparable(即给它们一个
compareTo(Person)
方法。如果完成了,您可以使用将 person 作为 SortedMap(如 TreeMap)的键。
如果可以通过多种方式比较两个人,请实现一个 Comparator作为单独的对象,
然后在构造时将此比较器提供给 SortedMap 。这不会对你
的 HashMap 进行排序(HashMap 总是具有看似随机的顺序),而是为你提供另一个排序的数据结构。
You need to be able to compare your Person objects. If there is a canonical way to compare them, let them implement
Comparable<Person>
(i.e. give them acompareTo(Person)
method.If this is done, you can use the persons as keys for a SortedMap (like TreeMap).
If there are multiple ways two persons could be compared, implement a
Comparator<Person>
as a separate object.Then give this comparator to the SortedMap on construction.
This will not sort your HashMap (a HashMap has always a seemingly random order), but give you another sorted datastructure instead.
首先,
TreeMap
按键排序,而不是按值排序。所以这已经对你有利了。在TreeMap
中用作键的任何对象必须实现Comparable
,或者您必须提供一个Comparator
作为构造函数参数。您所需要做的就是使用compareTo()
方法(来自Comparable
)或compare()
方法(来自Comparator
code>) 根据您的getAge()
属性进行比较。此处。
Comparator
将用于对映射中的键进行排序。First of all,
TreeMap
sorts on keys, not values. So that's already working in your favor. Any object you use as a key in aTreeMap
must implementComparable
, or you must provide aComparator
as a constructor argument. All you need to do is have yourcompareTo()
method (fromComparable
) orcompare()
method (fromComparator
) compare based on yourgetAge()
property.The
TreeMap
constructor that takes aComparator
is described here. TheComparator
will be used to sort the keys in the map.