5
The following code traverses a Set
and filters only the objects that the isActive()
is true
.
public Set<InvoiceLineDocument> getActiveLines() {
Set<InvoiceLineDocument> activeLines = new HashSet<>();
for (InvoiceLineDocument lineDocument : lineDocuments) {
if (lineDocument.isActive())
activeLines.add(lineDocument);
}
return activeLines;
}
How do I convert this implementation to Java 8 stream().filter()
?
Invoicelinedocument is your class or some lib?
– user28595
It’s a class of mine.
– Gustavo Piucco
Have you tried with
lineDocuments.stream().filter(x -> x.isActive()).collect(Collectors.toSet());
?.– Isac
Gosh, I hadn’t seen toSet(). That’s right, it worked. It wouldn’t be better to create an answer to mark as "solved"?
– Gustavo Piucco