golpebaixo baixo
golpebaixo baixo

Reputation: 46

How to set the equality comparison between two objects present in an ArrayList?

How can I set the equality comparison behavior between two objects whose class is defined by me that are stored in an object of type ArrayList?

Upvotes: 0

Views: 49

Answers (1)

Pr3ds
Pr3ds

Reputation: 358

For this there exists in the Java language two fundamental methods called equals (...) and hashCode () that are declared in class java.lang.Object by default and are part of the core library of Java.

You should implement these two methods in your classes that need to be compared!

The equals() method is used to compare if one object is equal to the other.

The hashCode() method is used to generate an ID number corresponding to the object.

See more in:https://docs.oracle.com/javase/7/docs/api/java/util/List.html

Upvotes: 2

Related Questions