# Use Arrays Stream
# Description
This rule transforms Arrays.asList(T..values).stream()
into either of the following:
- An unboxed specialized stream (i.e.,
IntStream
(opens new window),LongStream
(opens new window), orDoubleStream
(opens new window)). For example, the following stream:
Arrays.asList(1, 2, 3).stream()
is transformed to:
Arrays.stream(new int[]{1, 2, 3})
Thus, avoiding an unnecessary boxing of the elements of the stream.
- A stream generated by the short hand method
Stream.of(T... values)
(opens new window). For example, the following code:
Arrays.asList("1", "2", "3").stream()
is transformed to:
Stream.of("1", "2", "3")
# Benefits
Improves performance by avoiding unnecessary boxing of stream elements.
Improves readability by using short hand method Stream.of(T... values)
(opens new window) for generating a stream.
# Tags
# Code Changes
# Avoid Boxing of IntStream
Pre
Arrays.asList(1, 2, 3).stream()
.forEach(value -> consumePrimitiveInt(value));
Post
Arrays.stream(new int[] { 1, 2, 3})
.forEach(value -> consumePrimitiveInt(value));
# Using Stream.of
Pre
Arrays.asList("1", "2", "3").stream()
.forEach(value -> consumeString(value));
Post
Stream.of("1", "2", "3")
.forEach(value -> consumeString(value));
# Existing String Array to Stream
Pre
String [] stringArray = stringArrayFactory();
Arrays.asList(stringArray).stream()
.forEach(value -> consumeString(value));
Post
String [] stringArray = stringArrayFactory();
Stream.of(stringArray)
.forEach(value -> consumeString(value));
Use a Java Refactoring Tool
No license required
You can review this refactoring on your code without a license by installing jSparrow to your Eclipse IDE. Install the plug-in from Eclipse IDE: Eclipse Marketplace.
System-wide Refactoring
Do you want to automate this refactoring (and many more) to your system-wide code? The automatic application of this system-wide refactoring can be unlocked by acquiring your jSparrow license.