I usually print my ArrayLists such as
colors.forEach(color -> System.out.printf("color: %s ", color));
But I somehow can't apply this to normal Arrays (String[] colors
).
How do I apply this expression to normal Arrays?
I usually print my ArrayLists such as
colors.forEach(color -> System.out.printf("color: %s ", color));
But I somehow can't apply this to normal Arrays (String[] colors
).
How do I apply this expression to normal Arrays?
Basically you need a way to perform Stream operations on arrays. It's as simple as converting your array to a stream:
Arrrays.stream(colors).forEach(color -> System.out.printf("color: %s ", color));
For more info on this see Java 8 Stream and operation on arrays
The Arrays
class contains various methods for manipulating arrays, including the static stream()
method which returns a sequential Stream
with the specified array as its source. For your example, you can use the following code:
Arrays.stream(colors).forEach(color -> System.out.printf("color: %s ", color));