Reputation: 66
I have got a Set A, and a Set B. What I want to do is Set A - set B by using Set A.removeAll(Set B).
I have already overrided both equals() and hashCode() function as follows:
@Override
public int hashCode() {
return new HashCodeBuilder(17, 31) // two randomly chosen prime numbers
.append(postalcode)
.toHashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof GoogleGeometryPoint))
return false;
if (obj == this)
return true;
if(postalcode.equals(((GoogleGeometryPoint) obj).getPostalcode())) {
return true;
}
return false;
}
Set<GoogleGeometryPoint> onlinePostalcodes;
Set<GoogleGeometryPoint> offlinePostalcodes;
//Get all complaints
complaints =
CsvParser.parseCsv(Constants.FILE_KLACHTEN_CSV)
.stream()
.map(k -> CsvParser.mapToKlachtModel(k.toString()))
.collect(Collectors.toList());
//Get all offline postalcodes with coordinates
offlinePostalcodes =
CsvParser.parseCsv(Constants.FILE_POSTAL_CSV)
.stream()
.map(p -> CsvParser.mapToGoogleGeometryPoint(p.toString()))
.collect(Collectors.toSet());
//Copy complaints into set
onlinePostalcodes = complaints
.stream()
.map(e -> new GoogleGeometryPoint(e.getPostalcode(), e.getStreet(), e.getCity()))
.collect(Collectors.toSet());
//Remove all the postalcodes that are already offline available
onlinePostalcodes
.stream()
.filter(e -> e.getPostalcode() != null)
.collect(Collectors.toSet())
.removeAll(offlinePostalcodes);
After calling the removeAll() method, onlinePostalcodes seems to contain the same objects as before, nothing is removed. Is there anything wrong with my logic? Any help would be highly appreciated.
Upvotes: 0
Views: 95
Reputation: 35825
By using .collect(Collectors.toSet())
you create a new object from which you delete. You do not change onlinePostalcodes
. Maybe you intended to write
onlinePostalcodes = onlinePostalcodes
.stream()
.filter(e -> e.getPostalcode() != null)
.collect(Collectors.toSet());
onlinePostalcodes.removeAll(offlinePostalcodes);
Upvotes: 3