Comparing objects in Java
Note that we are talking about comparing whether two objects are the same or not!
For this there exist two fundamental methods called equals (...) and hashCode () that are declared in class java.lang.Object
by default and are part from the core Java library.
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 the same as the other.
The hashCode()
method is used to generate an ID number corresponding to the object.
Most of the standard Java language classes use these methods to insert and capture objects in a list, as well as to avoid duplication of objects such as HashSet.
The default implementation within the java.lang.Object
object uses the equals method to compare the memory address between objects, and the method returns "true" if both objects refer to / point to the same memory address.
But the language recommends that these methods be rewritten (Override) so that they define some logical or business way to compare the object. For example the class java.lang.String
Overwrite these methods to compare
its contents, to return "true" if two objects have the same string.
Some rules are recommended in implementation
Relationship contract between equals () and hashCode ()
-
If two objects are equal by the NullPointerException
method then the result of the equals()
method should be the same.
-
If two objects are not equal by the hashCode()
method then the result of equals()
can be the same or not.
Step-by-step to override the equals () method
Validate using hashCode()
, if equal return true
Validate if null, if null return false
Validate if the object is of the same type using this
, if not, return false
Try casting the object
Compare object attributes starting with numeric values. If they are not the same return false
OBS: Do not confuse this comparison with the comparison of magnitude of the values of an object; if one value is smaller or larger than the other, for example, in this case we would have to address the implementation of the Comparable and Comparator interfaces of Java. >
Example
import java.util.List;
import java.util.ArrayList;
public class Carro {
private String modelo;
private String cor;
private int ano;
public Carro(String modelo, String cor, int ano) {
this.modelo = modelo;
this.cor = cor;
this.ano = ano;
}
@Override
public boolean equals(Object o) {
if(this == o) return true;
if(o == null || getClass() != o.getClass()) return false;
Carro c = (Carro) o;
if(ano != c.ano) return false;
if(!modelo.equals(c.modelo)) return false;
return cor.equals(c.cor);
}
@Override
public int hashCode() {
int result = (modelo != null ? modelo.hashCode() : 0);
result = 31 * result + (cor != null ? cor.hashCode() : 0);
result = 31 * result + ano;
return result;
}
@Override
public String toString() {
return modelo + "," + cor + "," + ano;
}
public static void main(String args) {
List<Carro> listaCarros = new ArrayList<Carro>();
listaCarros.add(new Carro("Ford","Azul",2017))
listaCarros.add(new Carro("Honda","Preto",2016))
listaCarros.add(new Carro("Toyota","Branco",2015))
Carro meuCarro = new Carro("Honda","Preto",2016);
for(Carro carro : listaCarros) {
if(carro.equals(meuCarro)) {
System.out.println("O Carro "+carro+" é iqual ao meu!");
}
}
}
}
References