Para determinar si un objeto es diferente o igual a otro a veces es necesario compararlos con el método equals(). Cuando comparamos objetos compuestos creados por nosotros muchas veces esta comparación no es muy certera. Por tal motivo es muy útil sobrescribir los métodos equals() y hashCode() de la clase. Por suerte eclipse ya trae una utilidad que nos permite generarlos automáticamente sin necesidad de estar armándolos paso a paso.
Para realizar esto damos click derecho al proyecto, luego nos dirigimos a Source y finalmente a Generate hashCode() and equals()....
Por ejemplo al tener una clase como la siguiente:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Datos { | |
private int id; | |
private String nombre; | |
private String empresa; | |
public Datos(int id, String nombre, String empresa){ | |
this.id = id; | |
this.nombre = nombre; | |
this.empresa = empresa; | |
} | |
public int getId() { | |
return id; | |
} | |
public void setId(int id) { | |
this.id = id; | |
} | |
public String getNombre() { | |
return nombre; | |
} | |
public void setNombre(String nombre) { | |
this.nombre = nombre; | |
} | |
public String getEmpresa() { | |
return empresa; | |
} | |
public void setEmpresa(String empresa) { | |
this.empresa = empresa; | |
} | |
} |
Nos dará el siguiente resultado:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public int hashCode() { | |
final int prime = 31; | |
int result = 1; | |
result = prime * result + ((empresa == null) ? 0 : empresa.hashCode()); | |
result = prime * result + id; | |
result = prime * result + ((nombre == null) ? 0 : nombre.hashCode()); | |
return result; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (this == obj) | |
return true; | |
if (obj == null) | |
return false; | |
if (getClass() != obj.getClass()) | |
return false; | |
Datos other = (Datos) obj; | |
if (empresa == null) { | |
if (other.empresa != null) | |
return false; | |
} else if (!empresa.equals(other.empresa)) | |
return false; | |
if (id != other.id) | |
return false; | |
if (nombre == null) { | |
if (other.nombre != null) | |
return false; | |
} else if (!nombre.equals(other.nombre)) | |
return false; | |
return true; | |
} |
Ya con esto se pueden comparar dos instancias del mismo objeto cuando sea necesario y usará los atributos seleccionados para detectar si son iguales.
What is gambling? How does gambling work? | Dr.MCD
ResponderEliminarCasino Gaming is 춘천 출장마사지 an entertainment company based in Portland, 김포 출장마사지 Oregon. of a 제주 출장안마 sportsbook and sports betting and 통영 출장마사지 casino (a small 용인 출장안마 number of