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
65
votes
8 answers

How do I test if a variable does not equal either of two values?

I want to write an if/else statement that tests if the value of a text input does NOT equal either one of two different values. Like this (excuse my pseudo-English code): var test = $("#test").val(); if (test does not equal A or B){ do…
daGUY
  • 27,055
  • 29
  • 75
  • 119
65
votes
6 answers

Overriding Equals method in Structs

I've looked for overriding guidelines for structs, but all I can find is for classes. At first I thought I wouldn't have to check to see if the passed object was null, as structs are value types and can't be null. But now that I come to think of it,…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
64
votes
3 answers

Why BigDecimal("5.50") not equals to BigDecimal("5.5") and how to work around this issue?

Actually, I've found possible solution //returns true new BigDecimal("5.50").doubleValue() == new BigDecimal("5.5").doubleValue() Of course, it can be improved with something like Math.abs (v1 - v2) < EPS to make the comparison more robust, but…
Roman
  • 64,384
  • 92
  • 238
  • 332
64
votes
6 answers

How to compare two arrays in Kotlin?

Given some arrays in Kotlin let a = arrayOf("first", "second") val b = arrayOf("first", "second") val c = arrayOf("1st", "2nd") Are there built-in functions to the Kotlin std-lib that tests two arrays for (value) equality for each element? Thus…
Lars Blumberg
  • 19,326
  • 11
  • 90
  • 127
64
votes
12 answers

toString(), equals(), and hashCode() in an interface

So, I have an interface with a bunch of methods that need to be implemented, the method names are irrelevant. The objects that implement this interface are often put into collections, and also have a special toString() format that I want them to…
J_Y_C
  • 697
  • 1
  • 5
  • 7
63
votes
4 answers

c# enum equals() vs ==

In the case of using enums, is it better to use: if (enumInstance.Equals(MyEnum.SomeValue)) or to use if (enumInstance == MyEnum.SomeValue) Are their any important considerations using one vs the other?
ToddBFisher
  • 11,370
  • 8
  • 38
  • 54
62
votes
8 answers

Is it possible in java make something like Comparator but for implementing custom equals() and hashCode()

I have an array of objects and I want to concatenate it with another array of objects, except that objects that have same id's. That objects are used in many places in the system and don't have hash code or equals implemented. So I don't want to…
dhblah
  • 9,751
  • 12
  • 56
  • 92
61
votes
5 answers

Overriding GetHashCode for mutable objects?

I've read about 10 different questions on when and how to override GetHashCode but there's still something I don't quite get. Most implementations of GetHashCode are based on the hash codes of the fields of the object, but it's been cited that the…
Davy8
  • 30,868
  • 25
  • 115
  • 173
61
votes
9 answers

Understanding the workings of equals and hashCode in a HashMap

I have this test code: import java.util.*; class MapEQ { public static void main(String[] args) { Map m = new HashMap(); ToDos t1 = new ToDos("Monday"); ToDos t2 = new ToDos("Monday"); ToDos t3 = new…
andandandand
  • 21,946
  • 60
  • 170
  • 271
57
votes
12 answers

Force a class to override the .equals method

I have a bunch of class who implement a common interface : Command. And this bunch of class goes to a Map. To get the Map working correctly, I need to each class who implements Command to override the Object.equals(Object other) method. it's…
Antoine Claval
  • 4,923
  • 7
  • 40
  • 68
55
votes
5 answers

Is there a complete IEquatable implementation reference?

Many of my questions here on SO concerns IEquatable implementation. I found it being extremely difficult to implement correctly, because there are many hidden bugs in the naïve implementation, and the articles I found about it are quite incomplete.…
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
54
votes
3 answers

How to compare the length of a list in html/template in golang?

I am trying to compare the length of a list in golang html/template. But it is loading forever in html. {{ $length := len .SearchData }} {{ if eq $length "0" }} Sorry. No matching results found {{ end }} Could anyone help me with this?
Dany
  • 2,692
  • 7
  • 44
  • 67
53
votes
7 answers

What's the best strategy for Equals and GetHashCode?

I'm working with a domain model and was thinking about the various ways that we have to implement these two methods in .NET. What is your preferred strategy? This is my current implementation: public override bool Equals(object obj) { var newObj…
tucaz
  • 6,524
  • 6
  • 37
  • 60
52
votes
4 answers

What does the .= operator mean in PHP?

I have a variable that is being defined as $var .= "value"; How does the use of the dot equal function?
codacopia
  • 2,369
  • 9
  • 39
  • 58
50
votes
3 answers

How to use @EqualsAndHashCode With Include - Lombok

How to use @EqualsAndHashCode With Include, Lombok library for Java. @EqualsAndHashCode.Include( ) How to make Equals compare the class id? Example: @EqualsAndHashCode.Include( ) @Table(name = "USER") public class User { @Id …
Sérgio Thiago Mendonça
  • 1,161
  • 2
  • 13
  • 23