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
1
vote
1 answer

Is there a way to print dynamic variables?

I am coding in node.js atm. I need to create a dynamic variable and invoke it. e.g.: username = 'im_a_user'; global['ws[' + username + ']'] = ws; //(yes, i want to store the connection with ws module) but ws[im_a_user].send('blabla'); doesn't…
1
vote
1 answer

Using Object's hashCode() and equals() implementations after they have been overriden

Suppose I have: public class A { @Override public int hashCode() { // some expensive calculation } @Override public boolean equals(Object obj) { // some expensive calculation } } And at some point in my code…
Ofer
  • 293
  • 3
  • 9
1
vote
1 answer

Equal Height for all content box. Working fine on load, but not working on resizing window

Facing problem while resizing browser. equal_height function not working on resizing browser $(document).ready(function(){ function equal_height(){ var secHeight = $(".innter-con").map(function(){ return $(this).height(); }); …
maheshv13
  • 133
  • 9
1
vote
2 answers

Objects are equal but still added to hashSet?

i am adding objects to a hashSet , iam overriding hashcode() and equals() . i intentionally pass different instance variables in these hashcode is diff but objects are equal still it is inserting both in hashset code : HashSet hs = new…
Amol
  • 303
  • 4
  • 13
1
vote
2 answers

Access 'Not Equal To' Join

I have two tables, neither with a primary id. The same combination of fields uniquely identifies the records in each and makes the records between the two tables relate-able (I think). I need a query to combine all the records from one table and…
Quasimodo
  • 11
  • 2
1
vote
1 answer

jquery images with equal height

I am trying to write a Jquery function which will match the heights of all images on a page with the same class name. This works as expected providing the internet connection is decent. If the images take a while to load, then it fails to display…
Thomas Hughes
  • 99
  • 1
  • 11
1
vote
2 answers

Which one is better string.Equals() or == , in case of checking the equality of two strings in c#?

I have a checklist that asks me to use string.Equals() while checking the equality of two strings. And as far as I know, both Equals() and == yield same result when operated upon strings. For example shown below, string str1 = "Same…
pradeepradyumna
  • 952
  • 13
  • 34
1
vote
2 answers

MATLAB: combining cells based on same values within cell elements

I have a cell with values like: [1,2][4,5][2,8][1,9][5,2][6,7] Which I would like converted to: [1,2,4,5,8,9][6,7] Basically, where one value in one element is equal to another cell element value I would like both elements combined into a new…
user3470496
  • 141
  • 7
  • 33
1
vote
2 answers

Detecting Class object equivalence for classes loaded by different ClassLoaders

My Task is to write a unit test for a method findSubClassImplementation that returns an instance for a given Class object. The method signature looks like this: public T findSubClassImplementation(Class cls) throws…
thorwinn
  • 123
  • 1
  • 2
  • 9
1
vote
2 answers

all sums of different prime numbers equals 100

I have a homework to write a code, which find all sums of different prime numbers equals 100. I write this code for 2-elements sums, but i don't have idea how to iterate it for more elements. It has to be written in c++/clr. I would be happy if you…
Ziomal
  • 23
  • 4
1
vote
3 answers

Why Should Hibernate Entities outside of a Session have equals() and hashcode() implemented?

Section 2.1.5 of the Hibernate user guide states that In cases where you will be dealing with entities outside of a Session (whether they be transient or detached) ... you should consider implementing equals/hashCode. Ignoring the obvious reason…
aruuuuu
  • 1,605
  • 2
  • 22
  • 32
1
vote
3 answers

Java Comparing two identical objects gives false

I have a custom class called Card public class Card implements Serializable, Comparable{ private static final long serialVersionUID = 100L; private Rank rank; private Suit suit; public Card(Rank rank, Suit suit){ this.rank = rank; …
RnD
  • 1,019
  • 5
  • 23
  • 49
1
vote
1 answer

Implement hashCode and lazy-loading

This has been discussed a lot (e. g. here and there) and for me it sounds useful to use "business" keys when implementing the hashCode-method for Hibernate entities like: public class User { private String username; ... @Override …
mosquito87
  • 4,270
  • 11
  • 46
  • 77
1
vote
5 answers

equals and hashCode

I am running into a question about equals and hashCode contracts: here it is Given: class SortOf { String name; int bal; String code; short rate; public int hashCode() { return (code.length() * bal); } public boolean…
y62wang
  • 518
  • 1
  • 5
  • 14
1
vote
2 answers

EQ_SELF_USE_OBJECT Bug

I am writing an equals method in which two names are considered equal if they have the same first, middle, and last names. However, I keep getting the error "This class defines a covariant version of the equals() method, but inherits the normal…
neoiajr12
  • 13
  • 2
1 2 3
99
100