rspec/rules/S3631/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

51 lines
1.2 KiB
Plaintext

== Why is this an issue?
For arrays of objects, ``++Arrays.asList(T ... a).stream()++`` and ``++Arrays.stream(array)++`` are basically equivalent in terms of performance. However, for arrays of primitives, using ``++Arrays.asList++`` will force the construction of a list of boxed types, and then use _that_ list as a stream. On the other hand, ``++Arrays.stream++`` uses the appropriate primitive stream type (``++IntStream++``, ``++LongStream++``, ``++DoubleStream++``) when applicable, with much better performance.
=== Noncompliant code example
[source,java]
----
Arrays.asList("a1", "a2", "b1", "c2", "c1").stream()
.filter(...)
.forEach(...);
Arrays.asList(1, 2, 3, 4).stream() // Noncompliant
.filter(...)
.forEach(...);
----
=== Compliant solution
[source,java]
----
Arrays.asList("a1", "a2", "b1", "c2", "c1").stream()
.filter(...)
.forEach(...);
int[] intArray = new int[]{1, 2, 3, 4};
Arrays.stream(intArray)
.filter(...)
.forEach(...);
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Use "Arrays.stream" instead of "Arrays.asList".
=== Highlighting
``++Arrays.asList++``
endif::env-github,rspecator-view[]