Questions tagged [equals]

Refers to Java equals method, indicating whether some object is "equal to" this one.

General Introduction:

equals() method is defined in the java.lang.Object class, from which all other classes are derived, hence it's automatically defined for every class. However, it doesn't perform an intelligent comparison for user defined classes unless they override it. If it's not defined for a (user) class, it behaves the same as equality operator ==, which compares equality of references in Java.

The equals() method implements an equivalence relation on non-null object references:

  1. It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  2. It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  3. It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  4. It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  5. For any non-null reference value x, x.equals(null) should return false.

Hence, if any user defined class overrides equals() method then it should follow the above criteria for proper implementation.

Note that it is generally necessary to override the hashCode() method whenever this method is overridden, so as to maintain the general contract for the hashCode() method, which states that equal objects must have equal hash codes.


Further Reading:

  1. Javadoc for equals().

  2. Stackoverflow answers: Overriding equals and hashCode in Java , Java equals for a Class. Is == same as .equals,Java: equals and == and Should equals(Object) method be overridden when overriding hashCode() in java.

3150 questions
965
votes
82 answers

How to determine equality for two JavaScript objects?

A strict equality operator will tell you if two object types are equal. However, is there a way to tell if two objects are equal, much like the hash code value in Java? Stack Overflow question Is there any kind of hashCode function in JavaScript? is…
user4903
707
votes
20 answers

C# difference between == and Equals()

I have a condition in a silverlight application that compares 2 strings, for some reason when I use == it returns false while .Equals() returns true. Here is the code: if (((ListBoxItem)lstBaseMenu.SelectedItem).Content.Equals("Energy Attack")) { …
Drahcir
  • 12,311
  • 19
  • 63
  • 76
617
votes
11 answers

What issues should be considered when overriding equals and hashCode in Java?

What issues / pitfalls must be considered when overriding equals and hashCode?
Matt Sheppard
  • 116,545
  • 46
  • 111
  • 131
526
votes
31 answers

Why do I need to override the equals and hashCode methods in Java?

Recently I read through this Developer Works Document. The document is all about defining hashCode() and equals() effectively and correctly, however I am not able to figure out why we need to override these two methods. How can I take the…
Shashi
  • 12,487
  • 17
  • 65
  • 111
447
votes
8 answers

Comparing two strings, ignoring case in C#

Which of the following two is more efficient? (Or maybe is there a third option that's better still?) string val = "AStringValue"; if (val.Equals("astringvalue", StringComparison.InvariantCultureIgnoreCase)) OR if (val.ToLowerCase() ==…
pwmusic
  • 4,739
  • 4
  • 23
  • 14
447
votes
4 answers

Create the perfect JPA entity

I've been working with JPA (implementation Hibernate) for some time now and each time I need to create entities I find myself struggling with issues as AccessType, immutable properties, equals/hashCode, ... . So I decided to try and find out the…
Stijn Geukens
  • 15,454
  • 8
  • 66
  • 101
432
votes
22 answers

Getting an element from a Set

Why doesn't Set provide an operation to get an element that equals another element? Set set = ...; ... Foo foo = new Foo(1, 2, 3); Foo bar = set.get(foo); // get the Foo element from the Set that equals foo I can ask whether the Set contains…
foobar
  • 4,968
  • 3
  • 17
  • 11
408
votes
8 answers

Why would you use String.Equals over ==?

I recently was introduced to a large codebase and noticed all string comparisons are done using String.Equals() instead of == What's the reason for this, do you think?
JamesBrownIsDead
  • 4,573
  • 3
  • 20
  • 10
364
votes
16 answers

Equals(=) vs. LIKE

When using SQL, are there any benefits of using = in a WHERE clause instead of LIKE? Without any special operators, LIKE and = are the same, right?
Travis
  • 12,001
  • 8
  • 39
  • 52
325
votes
20 answers

Best implementation for hashCode method for a collection

How do we decide on the best implementation of hashCode() method for a collection (assuming that equals method has been overridden correctly) ?
Omnipotent
  • 27,619
  • 12
  • 30
  • 34
291
votes
9 answers

Compare two List objects for equality, ignoring order

Yet another list-comparing question. List list1; List list2; I need to check that they both have the same elements, regardless of their position within the list. Each MyType object may appear multiple times on a list. Is there a…
Bruno Teixeira
  • 3,099
  • 3
  • 16
  • 8
254
votes
6 answers

Check if bash variable equals 0

I have a bash variable depth and I would like to test if it equals 0. In case yes, I want to stop executing of script. So far I have: zero=0; if [ $depth -eq $zero ]; then echo "false"; exit; fi Unfortunately, this leads to: [: -eq: unary…
Perlnika
  • 4,796
  • 8
  • 36
  • 47
229
votes
4 answers

What's the difference between IEquatable and just overriding Object.Equals()?

I want my Food class to be able to test whenever it is equal to another instance of Food. I will later use it against a List, and I want to use its List.Contains() method. Should I implement IEquatable or just override Object.Equals()? From…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
197
votes
4 answers

BigDecimal equals() versus compareTo()

Consider the simple test class: import java.math.BigDecimal; /** * @author The Elite Gentleman * */ public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub …
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
196
votes
11 answers

Any reason to prefer getClass() over instanceof when generating .equals()?

I'm using Eclipse to generate .equals() and .hashCode(), and there is an option labeled "Use 'instanceof' to compare types". The default is for this option to be unchecked and use .getClass() to compare types. Is there any reason I should prefer…
Kip
  • 107,154
  • 87
  • 232
  • 265
1
2 3
99 100