Article
home/blog/Loading

The power of Stream.of() in Java

Stream.of() can be used instead of creating an array then iterating over it.

Java stream example

Stream.of(1,2,3).forEach(System.out::println);

Results in:

1

2

3

Even Better yet

If the order of execution does not matter you can execute Stream in .parallel()

Stream.of(1,2,3).parallel().forEach(System.out::println);

Results in an unpredictable output order such as:

3

1

2