rspec/rules/S2203/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

38 lines
919 B
Plaintext

== Why is this an issue?
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
[source,java]
----
List<String> bookNames = new ArrayList<>();
books.stream().filter(book -> book.getIsbn().startsWith("0"))
.map(Book::getTitle)
.forEach(bookNames::add); // Noncompliant
----
=== Compliant solution
[source,java]
----
List<String> bookNames = books.stream().filter(book -> book.getIsbn().startsWith("0"))
.map(Book::getTitle)
.collect(Collectors.toList());
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Use "collect(Collectors.toList())" instead of "forEach(xxx)".
endif::env-github,rspecator-view[]