19 lines
647 B
Plaintext
19 lines
647 B
Plaintext
While you can use either ``++forEach(list::add)++`` or ``++collect++`` with a ``++Stream++``, ``++collect++`` is by far the better choice because it's automatically thread-safe and parallellizable.
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
List<String> bookNames = new ArrayList<>();
|
|
books.stream().filter(book -> book.getIsbn().startsWith("0"))
|
|
.map(Book::getTitle)
|
|
.forEach(bookNames::add); // Noncompliant
|
|
----
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
List<String> bookNames = books.stream().filter(book -> book.getIsbn().startsWith("0"))
|
|
.map(Book::getTitle)
|
|
.collect(Collectors.toList());
|
|
----
|