I'm trying to convert Java loop code to Java 8 stream.
I have an ArrayList
of Row
objects that should sum all deliveredLength
excluding the Row
objects that has the same content as another Row
object that has the same content.
Java Loops
public int getDeliveredLength() {
List<Row> distinct = new ArrayList<>();
for (Row row : rows) {
if (sameContent(distinct, row)) {
continue;
}
distinct.add(row);
}
int sum = 0;
for (Row row : distinct) {
sum += row.getDeliveredLength();
}
return sum;
}
private boolean sameContent(List<Row> list, Row other) {
for (Row row : list) {
if (other.sameContent(row)) {
return true;
}
}
return false;
}
What would the Java 8 stream code be?
public int getDeliveredLength() {
return rows.stream().filter(??).map(??).mapToInt(Row::getDeliveredLength).sum()
}