Let us supposed to have ArrayList<Your own class>, to sort, binary search, remove you need to know equal. This is connected to implement equals method of Object.
Just you have implement equals, it is very easy to manipulate Collections. Internally in Collections classes, there are many usage of equals methods.
public class MetaConnectionData {
private String project;
private String subDomain;
private String port;
public MetaConnectionData(String _project, String _subDomain, String _port) {
this.project = _project;
this.subDomain = _subDomain;
this.port = _port;
}
public String getProject() {
return project;
}
public void setProject(String project) {
this.project = project;
}
public String getSubDomain() {
return subDomain;
}
public void setSubDomain(String subDomain) {
this.subDomain = subDomain;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public boolean equals(final Object other) {
if (this == other) {
return true;
} else if ((other != null) && (other.getClass() == getClass())) {
final MetaConnectionData otherData = (MetaConnectionData) other;
if (otherData.getPort().equals(port)
&& otherData.getProject().equals(project)
&& otherData.getSubDomain().equals(subDomain)) {
return true;
}
}
return false;
}
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
}
public int hashCode() {
return toString().hashCode();
}
}
'java core' 카테고리의 다른 글
How to get hostname in http url in Java (0) | 2009.04.28 |
---|---|
G1 garbage collection (0) | 2009.04.28 |
JavaOne 2008 (0) | 2009.04.17 |
addShutdownHook() of Runtime class (0) | 2009.04.17 |
JDK7에 clossloader의 close 메소드가 생기다니. (0) | 2009.04.09 |