
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.
50 lines
1.4 KiB
Plaintext
50 lines
1.4 KiB
Plaintext
== Why is this an issue?
|
|
|
|
There are two types of stream operations: intermediate operations, which return another stream, and terminal operations, which return something other than a stream. Intermediate operations are lazy, meaning they aren't actually executed until and unless a terminal stream operation is performed on their results. Consequently, if the result of an intermediate stream operation is not fed to a terminal operation, it serves no purpose, which is almost certainly an error.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java]
|
|
----
|
|
widgets.stream().filter(b -> b.getColor() == RED); // Noncompliant
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,java]
|
|
----
|
|
int sum = widgets.stream()
|
|
.filter(b -> b.getColor() == RED)
|
|
.mapToInt(b -> b.getWeight())
|
|
.sum();
|
|
Stream<Widget> pipeline = widgets.stream()
|
|
.filter(b -> b.getColor() == GREEN)
|
|
.mapToInt(b -> b.getWeight());
|
|
sum = pipeline.sum();
|
|
----
|
|
|
|
|
|
== Resources
|
|
|
|
* https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html#StreamOps[Stream Operations]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Refactor the code so this stream pipeline is used.
|
|
|
|
|
|
=== Highlighting
|
|
|
|
The intermediate stream function call
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|