I am trying to compare if two lists have the same objects, independent of its order and its number of elements.
For example:
List1 ["A", "B", "A"] and List2 ["A", "B"] must return equals.
I am using Set, because checks independently of the order of objects. However, returns false if both lists have a different number of elements.
Here is my code:
private Optional<EArea> getSimilarExistingArea(EImportArea importedArea) {
for (EArea existingArea : baseLineService.getBaseLine().getAreas()) {
EList<EPoint> importedAreaPoints = importedArea.getPoly().getPoints();
EList<EPoint> existingAreaPoints = existingArea.getPoly().getPoints();
final Set<EPoint> importedAreaPointsSET = new HashSet<>(importedAreaPoints);
final Set<EPoint> existingAreaPointsSET = new HashSet<>(existingAreaPoints);
if (existingAreaPointsSET.equals(importedAreaPointsSET)) return Optional.of(existingArea);
}
return Optional.empty();
}
I would like to return true if existingAreaPointsSET ["A", "B", "A"] and importedAreaPointsSET["A", "B"]
Here are the equals and hashCode funtions in the EPoint class:
@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof EPoint)) {
return false;
}
EPoint ePoint = (EPoint) o;
return Math.abs(Math.abs(ePoint.lat) - Math.abs(lat)) < EPSILON && Math.abs(Math.abs(ePoint.lon) - Math.abs(lon)) < EPSILON;
}
@Override
public int hashCode() {
return Objects.hash(lat, lon);
}