My attempt to customize the method contains of ArrayList, I used Eclipse to generate these methods inside a class TaskCandidateListItemDo
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result;
if (keyValue == null) {
result += 0;
} else {
result += keyValue.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;
}
TaskOverrideItemDo other = (TaskOverrideItemDo) obj;
if (keyValue == null) {
if (other.keyValue != null) {
return false;
}
} else if (!keyValue.equals(other.keyValue)) {
return false;
}
return true;
}
And here’s is how I use it
taskCandidateList = (List<TaskCandidateListItemDo>) namedQuery.list();
for (TaskOverrideItemDo taskOverrideItemDo : validateOverrideListSearchCriteriaDo
.getTaskOverrideItemDoList()) {
if (taskCandidateList.contains(taskOverrideItemDo.getKeyValue())) {
// do some code
}
}
My list declared as followed
private List<TaskCandidateListItemDo> taskCandidateList;
The // do some code inside the if statement is never executed even if keyValue exist. Also, the break point wouldnt stop at the equals method..
Try with:
Without
getKeyValue(), I suppose it returns the value of the fieldkeyValue(and yourequalsmethod compares onlyTaskCandidateListItemDobecause of:As alternative you may support comparison with the type of
keyValueinside theequalsmethod.