I've been playing around with streams and then I noticed that when I do the following, it won't produce an output into the console:
String[] is = {"Hello", "World", "My", "Name", "Is", "Jacky"};
Arrays.stream(is).peek(s -> System.out.println(s));
I figure this is because peek()
is a non-terminating stream method, and forEach()
should be used instead of peek()
to terminate the stream and produce the results:
Arrays.stream(is).forEach(s -> System.out.println(s));
Is there however a way to terminate a stream 'early', using perhaps a custom terminating method (functional interface) that wouldn't do anything except for terminating the stream?.. Is there a proper way to do this with what is already present in Java?
I understand I could do something like this:
Arrays.stream(is).peek(s -> System.out.println(s)).forEach(s -> {});
But that feels wasteful.