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
50
votes
6 answers

Equals(item, null) or item == null

Is code that uses the static Object.Equals to check for null more robust than code that uses the == operator or regular Object.Equals? Aren't the latter two vulnerable to being overridden in such a way that checking for null doesn't work as expected…
Joseph Sturtevant
  • 13,194
  • 12
  • 76
  • 90
49
votes
4 answers

Objects.equals and Object.equals

I try to create a tuple class that allows a tuple-like structure in Java. The general type for two elements in tuple are X and Y respectively. I try to override a correct equals for this class. Thing is, I know Object.equals falls into default that…
jfcjohn
  • 611
  • 1
  • 5
  • 6
49
votes
5 answers

Does Java 7 switch statement with String use equals() method?

Java 7 supports switching with Strings like the code below switch (month.toLowerCase()) { case "january": monthNumber = 1; break; case "february": monthNumber = 2; break; default: monthNumber = 0; break; } Does Java call…
Narendra Pathai
  • 41,187
  • 18
  • 82
  • 120
48
votes
6 answers

String.Equals() not working as intended

I am using LINQ to search through one of my Entity Framework tables and find a "group" based on the name. The name is a string and appears to be Unicode (says it is in the edmx). I have a method GetGroup() and I pass in a name to search for.…
Travyguy9
  • 4,774
  • 8
  • 43
  • 63
47
votes
5 answers

Hashcode and Equals for Hashset

Please clarify my doubt in Hashset. Consider the following code, class Person { String name; Person(String n) { name=n; } public String getName() { return name; } @Override public boolean…
Lolly
  • 34,250
  • 42
  • 115
  • 150
47
votes
6 answers

Overloading operator== versus Equals()

I'm working on a C# project on which, until now, I've used immutable objects and factories to ensure that objects of type Foo can always be compared for equality with ==. Foo objects can't be changed once created, and the factory always returns the…
JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169
46
votes
3 answers

How to compare two Strings when both can be null?

I am aware that it is better to call the equals method over using the == operator (see this question). I want two strings to compare as equal if they are both null or if they represent the same string. Unfortunately the equals method will throw an…
Benjy Kessler
  • 7,356
  • 6
  • 41
  • 69
45
votes
12 answers

Java comparison with == of two strings is false?

String parts is String[6]: ["231", "CA-California", "Sacramento-155328", "aleee", "Customer Service Clerk", "Alegra Keith.doc.txt"] But when I compare parts[0] with "231": "231" == parts[0] the above result is false, I'm confused, so could…
omg
  • 136,412
  • 142
  • 288
  • 348
45
votes
7 answers

Compare two objects with a check for null

Is there a method in the JDK that compares two objects for equality, accounting for nulls? Something like this: public static boolean equals(Object o1, Object o2) { if (o1 == null) { return o2 == null; // Two nulls are considered…
dcstraw
  • 3,243
  • 3
  • 29
  • 38
44
votes
3 answers

Is there a way to check if two Collections contain the same elements, independent of order?

I've been looking for a method that operates like Arrays.equals(a1, a2), but ignoring the element order. I haven't been able to find it in either Google Collections (something like Iterables.elementsEqual(), but that does account for ordering) and…
Jorn
  • 20,612
  • 18
  • 79
  • 126
44
votes
4 answers

Is there a way to auto-generate GetHashCode and Equals with ReSharper?

In eclipse, when I code in Java, there is a feature to auto-generate a basic, efficient, and bug free implementation of hashCode() and equals() without consuming brain power. Is there a similar feature either built-in in Visual Studio or in…
Samuel Rossille
  • 18,940
  • 18
  • 62
  • 90
43
votes
17 answers

Setting equal heights for div's with jQuery

I want to set equal height for divs with jQuery. All the divs may have different amount of content and different default height. Here is a sample of my html layout:
This is
the highest
Mitya Ustinov
  • 903
  • 3
  • 11
  • 17
42
votes
8 answers

What is the hashCode for a custom class having just two int properties?

In Java, I have a class that represents a point with int coordinates public class Point { int x = -1; int y = -1; public Point (int xNew, int yNew) { x = xNew; y = yNew; } public boolean equals (Object o) { //…
Sophie Sperner
  • 4,428
  • 8
  • 35
  • 55
41
votes
5 answers

C# - compare two SecureStrings for equality

I have a WPF application with two PasswordBoxes, one for the password and another for the password to be entered a second time for confirmation purposes. I was wanting to use PasswordBox.SecurePassword to get the SecureString of the password, but I…
Sarah Vessels
  • 30,930
  • 33
  • 155
  • 222
41
votes
3 answers

How to teach eclipse to generate compact equals() and hashCode() from the jdk 7 Objects class?

Some days ago we switched to Java 7 within my Company - finally! Jay \o/ So I found out about the Objects class and was astonished how short the methods hashCode() and equals() were realized, reducing a lot of boylerplate code compared to the ones…
Aufwind
  • 25,310
  • 38
  • 109
  • 154