Creating Streams
There are multiple ways of creating streams in Java; the simplest of these is by using the Stream.of()
function. This function can take either a single object or multiple objects in varargs
:
Stream<Object> objectStream = Stream.of(new Object());
If you have multiple objects in your stream, then use the varargs
version:
Stream<Object> objectStream = Stream.of(new Object(), new Object(), new Object());
The primitive versions of these streams work in an identical fashion; just replace the Object
instances with integers, longs, or doubles.
You can also create streams from different collections—for example, lists and arrays. Creating a stream from a list will look like this:
List<String> stringList = List.of("string1", "string2", "string3"); Stream<String> stringStream = stringList.stream();
To create a stream from an array of items, you can use the Arrays
class, just like the primitive versions...