9. Streams Flashcards
Can you use a stream again after you closed it with a terminal operation?
No. You can’t use that stream again and you need to create a new one.
What is a Stream?
In java a stream is an object that gets its data from a source, but it doesn’t store any data itself
How to create a stream?
You can create a stream by calling the .stream() method This method is a default method of the collection interface and so inherited by all classes that implement Collection
Can we Stream a Map (Map doesn’t inherit from Collection)?
If you want to stream a map, you must first use the entrySet() method to turn the Map into a Set
How to build a stream with stream.of()?
Stream.of() is quite flexible; it works with any object values, so you can create a Stream of String, Integers, Doubles, etc. The method meaning is you can supply the of() method with any number of arguments, and you get back an ordered stream of those values.
How to create a stream from an array?
You can create a Stream from an Array as follows: Arrays.stream(array). Another way to create a stream from an array is to use the Stream.of() method.
How to create a stream from a file?
Using a stream to process data in a file is easy. You can use Files to read data from a file; the static lines() method of Files returns a Stream, so we can stream the data using the file as the source. So to create a stream from a file you write: Stream stream = Files.lines(Paths.get(filename));
What are primitive streams?
There are also primitive streams designed to avoid autoboxing, for doubles, ints and longs. These are DoubleStream, IntStream and LongStream. So you can create a DoubleStream like this: DoubleStream s3 = DoubleStream.of(392.32, 3942.42, 283.34, 2324,3)
Keep in mind the difference between a Stream and DoubleStream. The first is a stream of Double objects, and the second is a stream of double values. If you create a list and then stream it, the stream type is Stream
Why do you wanna use a stream?
The main reason to use streams is when you start doing multiple intermediate operation. The big deal is that streams use something called a “pipeline”, which can, in some circumstances, dramatically improve the efficiency of data processing.
How is a stream build up?
A stream pipeline consists of three parts: a source, zero or more intermediate operations, and a terminal operation. The source describes where the data is coming from; the intermediate operations operate on the stream and produce another, perhaps modified, stream; and the terminal operation ends the stream and typically produces a value or some output.
What do they mean with streams are lazy?
If you don’t include a terminal operation but you have got everything set up (intermadiate operations), you actually don’t have anything to kickstart the data processing. Until a terminal operation is executed nothing does happen. So streams are lazy because no data is flowing, even though it helps us to think about streams that way.
What does the streams map() method do?
The Streams’s map() transform elements of a stream. The map() method takes a Function. The function’s apply() takes one value and produce another value (not necessarily of the same type). The apply() that you pass into the map() gets called by map() behind the scenes, and the value returned from apply gets passed to the next operation in the stream pipeline.
How to map a primitive?
If you want to get a primtive back instead of an object, you can the Stream.mapToDouble() method that takes a ToDoubleFunction, who applyAsDouble() method takes an object and returns a double.
What is an OptionalDouble?
An OptionalDouble is a double value that might or might not
be there, so it doesn’t return null or 0.0, instead we get back an OptionalDouble, which is a double value that might or might not be there.
How do you get a double value out of an OptionalDouble?
You use getAsDouble() method. If you try to call getAsDouble() on an empty OptionalDouble you will get a NoSuchElementException.