简体   繁体   English

Java链表.contains方法

[英]Java Linked List .contains method

I have a LinkedList containing elements of type A. I need to check whether the list contains an element based on some criteria. 我有一个LinkedList,其中包含类型A的元素。我需要检查列表是否包含基于某些条件的元素。

Is it enough to override the .equals method in class A, or do I need to override the hash method as well? 覆盖类A中的.equals方法是否足够,还是我也需要覆盖哈希方法?

You need to override the hashCode() method when your Object will be used in data structures that use hashes. 在将对象用于使用哈希的数据结构中时,您需要覆盖hashCode()方法。 HashMap, HashSet, etc. HashMap,HashSet等

Nothing is saying that you must implement the hashCode() function. 没什么说您必须实现hashCode()函数。 Many data structures only use the equals() method, not the hashCode() function, so you can get away with not implementing it. 许多数据结构仅使用equals()方法,而不使用hashCode()函数,因此您可以不必执行它。

But you can't really guarantee that nobody will ever put it into another data structure that does use the hashCode() function, so it's probably a good habit to just implement it from the start. 但是您不能真正保证没有人会把它放入使用hashCode()函数的另一个数据结构中,因此从一开始就实现它可能是一个好习惯。

You don't HAVE to override hashCode, but equal Objects should have the same hashcode. 您不必重写hashCode,但是相等的Objects应该具有相同的hashcode。 My advice: If you use eclipse, rightclick on the class, go to source, generate equals() and hashcode(), and select the variables that have to be equal. 我的建议:如果使用eclipse,请右键单击该类,转到源代码,生成equals()和hashcode(),然后选择必须相等的变量。 Its the easiest way. 这是最简单的方法。

The equals() method should be used only to tell what the object's identity is. equals()方法仅应用于告知对象的标识是什么。

If the criteria used to find the object you're looking for match perfectly those used to define the object's identity, then it is just fine to call List 's contains() method, which relies on equals() . 如果用于查找您要查找的对象的条件与用于定义对象标识的条件完全匹配,则可以调用依赖于equals() Listcontains()方法。

In any other case, you should rather loop over the list and perform manual comparisons. 在任何其他情况下,您都应该遍历列表并进行手动比较。 Or alternatively, you use Java8 Streams, which offer filtering functions. 或者,您可以使用Java8 Streams,它提供过滤功能。

You must be aware that by implementing the equals method, you're defining an equivalence relation, ie one that subdivides all objects of this class into equivalence classes. 您必须意识到,通过实现equals方法,您正在定义一个等价关系,即将此类的所有对象细分为等价类的关系。 When there are many objects that fulfill your criteria, they would all be considered equal and they would all have to have the same hashCode. 当有许多满足您条件的对象时,它们都将被视为相等,并且都必须具有相同的hashCode。 As a consequence, when you put those objects in a HashMap at some point, all these objects would end up in the same slot, leading to a degenerated HashMap that would perform badly. 结果,当您将这些对象放在某个位置的HashMap中时,所有这些对象最终都将位于同一插槽中,从而导致性能下降的退化HashMap。

Even if you do not plan to put your objects in a HashMap, this seems to be a strong argument against implementing equals to compare based on a broader criteria. 即使您不打算将对象放在HashMap中,这似乎也是反对基于更广泛的标准实现equals比较的强烈理由。

You have to make sure that your implementation fulfills the contact of equivalence, that is, it must be reflexive, symmetric, and transitive. 您必须确保您的实现满足对等关系,也就是说,它必须是自反的,对称的和可传递的。 Joshua Bloch's standard book " Effective Java " has a detailed explanation. 约书亚·布洛赫(Joshua Bloch)的标准书“ 有效的Java ”有详细的解释。

You have to implement hashCode whenever you implement equals , because every two objects that are equal must have the same hash code (see this post for a detailed explanation). 每当实现equals就必须实现hashCode ,因为equals每两个对象必须具有相同的哈希码(有关详细说明,请参见此帖子 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM