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.

2982 questions
760
votes
67 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
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
  • 111,039
  • 46
  • 105
  • 128
594
votes
18 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
  • 10,961
  • 18
  • 58
  • 75
438
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
  • 11,342
  • 16
  • 60
  • 108
435
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
  • 14,803
  • 7
  • 60
  • 100
370
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,338
  • 3
  • 14
  • 10
364
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
  • 3,869
  • 4
  • 21
  • 14
353
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
  • 3,993
  • 3
  • 18
  • 10
311
votes
15 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
  • 10,601
  • 8
  • 37
  • 52
309
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
  • 25,231
  • 11
  • 28
  • 33
263
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
  • 2,819
  • 3
  • 14
  • 8
203
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,014
  • 7
  • 32
  • 43
200
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
  • 90,453
  • 117
  • 313
  • 521
181
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
  • 99,109
  • 82
  • 222
  • 258
178
votes
27 answers

How to check if my string is equal to null?

I want to perform some action ONLY IF my string has a meaningful value. So, I tried this. if (!myString.equals("")) { doSomething } and this if (!myString.equals(null)) { doSomething } and this if ( (!myString.equals("")) &&…
Roman
  • 97,757
  • 149
  • 317
  • 426
1
2 3
99 100