排序集合的Java对象排序 [英] Java Object Ordering for Sorted Collections

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

问题描述

当我查看 Java对象排序



通过定义一个类 Employee 它本身是可比的员工的名字,教程不显示这个类是否覆盖了 equals 方法。然后,它使用自定义的 Comparator ,其中员工按照资历排序,对员工名单进行排序,我可以理解。



然后教程解释了为什么这将不适用于排序的集合,例如 TreeSet (a SortedSet ),原因是:


它会产生与equals不兼容的排序。这意味着此Comparator等于equals方法不会的对象。特别是,在同一天被雇用的任何两个雇员将比较平等。当你排序列表时,这没关系;但是当您使用Comparator来排序已排序的集合时,它是致命的。如果使用此Comparator将在同一日期雇用的多个雇员插入到TreeSet中,则只有第一个将添加到集合;第二个将被视为重复的元素,将被忽略。


现在我很困惑,因为我知道 List 允许重复元素,而 设置 不基于 equals 方法。所以我不知道什么时候教程说由 Comparator 生成的排序与equals不兼容,是什么意思?它还说,如果你使用这个比较器插入多个雇员在同一日期到一个TreeSet中,只有第一个将被添加到集合; 我不明白如何使用 Comparator 会影响原始等于方法。我想我的问题是如何 TreeSet 将产生和排序在这种情况下,当比较 equals 使用方法。

解决方案

当教程说Comparator生成的排序与equals不兼容时,是什么意思?


在本例中, code> Comparator 根据他们的资历比较两个 Employee 对象。此比较不以任何方式使用 equals hashCode 。记住这一点,当我们将 Comparator 传递给 TreeSet 时,集合将考虑来自 Comparator 为等同。因此,如果 Employee 共享开始日期,则只会添加一个,因为该集合认为它们相等。



最后:


我想我的问题是如何在这种情况下生成和排序TreeSet,当使用compare和equals方法时。


对于 TreeSet ,如果 Comparator ,它使用 compare 方法来确定对象的相等性和排序。如果没有给出 Comparator ,那么集合使用正在排序的对象的 compareTo 方法(它们必须实现可比较)。



Java规范声称比较 / 使用的compareTo 方法必须符合 equals 是因为 / code>规范使用 equals ,即使这种特定类型的 Set TreeSet ,使用比较。



如果您收到 Set 从一些方法实现,你可以预期在等于定义的 Set 中没有对象的重复,方法。因为 TreeSet 不使用此方法,开发人员必须小心确保比较方法产生与 equals


When I'm looking at the Java Object Ordering tutorial, the last section 'Comparators' of the article confused me a little bit.

By defining a class Employee which itself is comparable by employee's name, the tutorial doesn't show if this class has overridden the equals method. Then it uses a customized Comparator in which the employees are sorted by the seniority to sort a list of employees and which I could understand.

Then the tutorial explains why this won't work for a sorted collection such as TreeSet (a SortedSet), and the reason is:

it generates an ordering that is not compatible with equals. This means that this Comparator equates objects that the equals method does not. In particular, any two employees who were hired on the same date will compare as equal. When you're sorting a List, this doesn't matter; but when you're using the Comparator to order a sorted collection, it's fatal. If you use this Comparator to insert multiple employees hired on the same date into a TreeSet, only the first one will be added to the set; the second will be seen as a duplicate element and will be ignored.

Now I'm confused, since I know List allows duplicate elements while Set doesn't based on equals method. So I wonder when the tutorial says the ordering generated by the Comparator is not compatible with equals, what does it mean? And it also says 'If you use this Comparator to insert multiple employees hired on the same date into a TreeSet, only the first one will be added to the set; the second will be seen as a duplicate element and will be ignored.' I don't understand how using a Comparator will affect the use of original equals method. I think my question is how the TreeSet will be produced and sorted in this case and when the compare and equals methods are used.

解决方案

So I wonder when the tutorial says the ordering generated by the Comparator is not compatible with equals, what does it mean?

In this example, the Comparator compares two Employee objects based on their seniority alone. This comparison does not in any way use equals or hashCode. Keeping that in mind, when we pass this Comparator to a TreeSet, the set will consider any result of 0 from the Comparator as equality. Therefore, if any Employees share starting dates, only one will be added because the set thinks they are equal.

Finally:

I think my question is how the TreeSet will be produced and sorted in this case and when the compare and equals methods are used.

For the TreeSet, if a Comparator is given, it uses the compare method to determine equality and ordering of objects. If no Comparator is given, then the set uses the compareTo method of the objects being sorted (they must implement Comparable).

The reason why the Java specification claims that the compare/compareTo method being used must be in line with equals is because the Set specification makes use of equals, even though this specific type of Set, the TreeSet, uses comparisons instead.

If you ever receive a Set from some method implementation, you can expect that there are no duplicates of the objects in that Set as defined by the equals method. Because TreeSet doesn't use this method, however, developers must be careful to ensure that the comparison method results in the same equality as equals does.

这篇关于排序集合的Java对象排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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