rspec/rules/S3631/java/rule.adoc

43 lines
1.1 KiB
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
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.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
Arrays.asList("a1", "a2", "b1", "c2", "c1").stream()
.filter(...)
.forEach(...);
Arrays.asList(1, 2, 3, 4).stream() // Noncompliant
.filter(...)
.forEach(...);
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
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)
include::message.adoc[]
include::highlighting.adoc[]
endif::env-github,rspecator-view[]