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
41
votes
4 answers

Arrays Equals Ignoring Order

Possible Duplicate: Java: Checking equality of arrays (order doesnt matter) I have two arrays : String[] a1 = {"a", "b", "c"}; String[] a2 = {"c", "b", "a"}; I need to check if both contains same elements (and of same length) irrespective of…
Anil Bharadia
  • 2,760
  • 6
  • 34
  • 46
41
votes
2 answers

Linq: What is the difference between == and equals in a join?

I always wondered why there's an equals keyword in linq joins rather than using the == operator. Property deadline = (from p in properties join w in widgets on p.WidgetID equals w.ID select p).First(); Instead of Property deadline = (from p…
Michael Klement
  • 3,376
  • 3
  • 30
  • 34
40
votes
5 answers

Is there a __equals method in PHP like there is in Java?

Is there a pattern or magic method you can use in PHP to define when to compare two instances of a class? For example, in Java I could easily override the equals method and create a custom way of checking and compare two instances.
Simon
  • 1,643
  • 7
  • 30
  • 61
39
votes
4 answers

Can I check strings equality in lua?

Just a straight forward beginner question, I am coding Lua stuff for Garrys Mod, learning by reading wiki and other codings. if (self.Owner:SteamID( ) == "STEAM_0:1:44037488" ) then the above is the code I want to use, to check to see if the STEAM…
Howard Sun
  • 399
  • 1
  • 3
  • 3
38
votes
6 answers

Why Java does not see that Integers are equal?

I have integers that are supposed to be equal (and I verify it by output). But in my if condition Java does not see these variables to have the same value. I have the following code: if (pay[0]==point[0] && pay[1]==point[1]) { …
Roman
  • 124,451
  • 167
  • 349
  • 456
38
votes
4 answers

List.contains() fails while .equals() works

I have an ArrayList of Test objects, which use a string as the equivalency check. I want to be able to use List.contains() to check whether or not the list contains an object that uses a certain string. Simply: Test a = new Test("a"); a.equals("a");…
idlackage
  • 2,715
  • 8
  • 31
  • 52
37
votes
4 answers

Implementing equals and hashCode for objects with circular references in Java

I have two classes defined such that they both contain references to the other object. They look similar to this (this is simplified; in my real domain model class A contains a list of B and each B has a reference back to parent A): public class A…
Tom
  • 3,006
  • 6
  • 33
  • 54
37
votes
6 answers

Why assertEquals and assertSame in junit return the same result for two instances same class?

According to documentation assertEquals() Asserts that two objects are equal. assertSame() Asserts that two objects refer to the same object. So I am expecting that if I have a class like below class SomeClass {} then SomeClass someClass1= new…
rawData
  • 729
  • 4
  • 10
  • 17
36
votes
2 answers

XPath operator "!=". How does it work?

XML document: Hello! How would you evaluate the following XPath queries? /doc/A/Node != 'abcd' /doc/B/Node != 'abcd' …
Keith
  • 361
  • 1
  • 3
  • 3
36
votes
5 answers

Why should I override hashCode() when I override equals() method?

Ok, I have heard from many places and sources that whenever I override the equals() method, I need to override the hashCode() method as well. But consider the following piece of code package test; public class MyCustomObject { int intVal1; …
bragboy
  • 34,892
  • 30
  • 114
  • 171
36
votes
4 answers

MySQL - NULL safe NOT equal operator

I am just curious - I know about NULL safe equal operator <=>, but is there some NULL safe NOT equal operator? Or I have to always use something like this: (tab.id != 1 OR tab.id IS NULL) or someone prefers !(tab.id <=> 1)
Koralek M.
  • 3,231
  • 4
  • 26
  • 32
35
votes
3 answers

.Contains() method not calling Overridden equals method

I am having an issue where I make an ArrayList of Foo objects, I override the equals method, and I cannot get the contains method to call the equals method. I have tried overriding equals and hashcode together, but it still doesn't work. I'm sure…
Reid Mac
  • 2,411
  • 6
  • 37
  • 64
35
votes
3 answers

What is the standard idiom for implementing equals and hashCode in Scala?

What is the standard idiom for implementing the equals and hashCode methods in Scala? I know the preferred approach is discussed in Programming in Scala, but I don't currently have access to the book.
Jim Hurne
  • 7,187
  • 4
  • 44
  • 44
35
votes
8 answers

why equals() method when we have == operator?

When i see the implementation of equals() method it does nothing but same as what == does. So my question is what was the need to have this as separate method when we have == operator which does the same work?
GuruKulki
  • 25,776
  • 50
  • 140
  • 201
35
votes
5 answers

Is it proper for equals() to depend only on an ID?

Let's suppose I have class User: public class User { private Long id; private String name; private Integer age; private BigDecimal account; // other fields, getters and setters } Is it proper to override the equals method as…
Michael
  • 1,152
  • 6
  • 19
  • 36