Java 为什么自定义比较器不能与 hashSet 一起使用来检查重复对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21362616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:17:45  来源:igfitidea点击:

Why can't custom comparator be used with hashSet to check duplicate objects

javaeclipsehashset

提问by rhozet

I have defined Comparator in Mobile class for comparing. Comparing criteria, i have used the id field of Mobile.

我在 Mobile 类中定义了 Comparator 进行比较。比较标准,我使用了Mobile的id字段。

Eclipse shows an error for first one(hashSet):(Syntax error) while for treeset it works perfectly.

Eclipse 显示第一个 (hashSet):(Syntax error) 的错误,而对于 treeset,它可以完美运行。

HashSet<Mobile> mobileSet = new HashSet(new Mobile().new Comparator())
    TreeSet<Mobile> ts = new TreeSet<Mobile>(new Mobile().new Comparator());

Can someone help me with this.

有人可以帮我弄这个吗。

As a result to use hashset, i need to override the equals and hashcode in BaseClass(Mobile)

由于使用hashset,我需要覆盖BaseClass(Mobile)中的equals和hashcode

采纳答案by Marko Topolnik

HashSet<Mobile> mobileSet = new HashSet(new Mobile().new Comparator())

Let's enumerate several things which are wrong with that line of code:

让我们列举这行代码的几个错误:

  • it is missing the ending semicolon (your "syntax error");
  • it is missing the generic type parameter (or diamond operator) on new HashSet;
  • it is using a constructor argument of incompatible type with HashSet(Collection<? extends E> coll).
  • 它缺少结尾的分号(您的“语法错误”);
  • 它缺少泛型类型参数(或菱形运算符)new HashSet
  • 它正在使用与 不兼容类型的构造函数参数HashSet(Collection<? extends E> coll)

The Javadoc of HashSetexplains how a hash set works. It should be very easy to realize it has nothing to do with Comparators.

的 JavadocHashSet解释了哈希集的工作原理。应该很容易意识到它与Comparators无关。

回答by Maciej Cygan

Hashset is designed not to accept duplicates !. So if your set does not contain given element it will add it to the set. If however in your set such element appears 2nd (same) element will not be added and will be discarded.

Hashset 旨在不接受重复项!。因此,如果您的集合不包含给定元素,它会将其添加到集合中。但是,如果在您的集合中出现此类元素,则不会添加第二个(相同)元素并将被丢弃。

HashSet:

哈希集:

class offers constant time performance for the basic operations (add, remove, contains and size). it does not guarantee that the order of
elements will remain constant over time iteration performance depends on the initial capacity and the load factor of the HashSet. It's
quite safe to accept default load factor but you may want to specify
an initial capacity that's about twice the size to which you expect
the set to grow.

类为基本操作(添加、删除、包含和大小)提供恒定的时间性能。它不保证
元素的顺序会随着时间的推移保持不变迭代性能取决于初始容量和 HashSet 的负载因子。
接受默认负载因子是非常安全的,但您可能希望指定
一个初始容量,该容量大约是您期望
集增长到的大小的两倍。

TreeSet:

树集:

guarantees log(n) time cost for the basic operations (add, remove and contains) guarantees that elements of set will be sorted (ascending, natural, or the one specified by you via it's constructor) doesn't offer any tuning parameters for iteration performance offers a few handy methods to deal with the ordered set like first(), last(), headSet(), and tailSet() etc

保证基本操作(添加、删除和包含)的 log(n) 时间成本保证集合的元素将被排序(升序、自然或您通过其构造函数指定的元素)不提供任何迭代调整参数performance 提供了一些方便的方法来处理有序集合,如 first()、last()、headSet() 和 tailSet() 等

Important points:

要点:

Both guarantee duplicate-free collection of elements It is generally faster to add elements to the HashSet and then convert the collection to a TreeSet for a duplicate-free sorted traversal. None of these implementation are synchronized. That is if multiple threads access a set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. LinkedHashSet is in some sense intermediate between HashSet and TreeSet. Implemented as a hash table with a linked list running through it, however it provides insertion-ordered iteration which is not same as sorted traversal guaranteed by TreeSet.

两者都保证元素的无重复集合 通常将元素添加到 HashSet 中,然后将集合转换为 TreeSet 进行无重复排序遍历通常会更快。这些实现都不是同步的。也就是说,如果多个线程同时访问一个集合,并且至少有一个线程修改了该集合,则必须在外部进行同步。LinkedHashSet 在某种意义上介于 HashSet 和 TreeSet 之间。实现为一个带有链表的哈希表,但它提供了插入顺序迭代,这与 TreeSet 保证的排序遍历不同。