9. Streams Flashcards

1
Q

Can you use a stream again after you closed it with a terminal operation?

A

No. You can’t use that stream again and you need to create a new one.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a Stream?

A

In java a stream is an object that gets its data from a source, but it doesn’t store any data itself

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How to create a stream?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Can we Stream a Map (Map doesn’t inherit from Collection)?

A

If you want to stream a map, you must first use the entrySet() method to turn the Map into a Set

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to build a stream with stream.of()?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to create a stream from an array?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to create a stream from a file?

A

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));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are primitive streams?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why do you wanna use a stream?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How is a stream build up?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What do they mean with streams are lazy?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the streams map() method do?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to map a primitive?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is an OptionalDouble?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you get a double value out of an OptionalDouble?

A

You use getAsDouble() method. If you try to call getAsDouble() on an empty OptionalDouble you will get a NoSuchElementException.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Do the min() and max() functions need a comparator?

A

Yes, except for DoubleStream.min().max()

17
Q

What does the ifPresent() do?

A

If you have an optional value, we need to first test to see whether it is present, with isPresent(),
before trying to get that Double value from the Optional.

18
Q

How to get data from the Optional?

A
  • Get a value from an Optional: get()
  • Get a value from an OptionalDouble: getAsDouble()
  • Get a value from an OptionalInt: getAsInt()
  • Get a value from an OptionalLong: getAsLong()
19
Q

What is another way to test if an optional contains a value? and if it does get that value?

A

Another way to test to see whether an optional contains a value and, if it does, to then get that
value, is the ifPresent() method. This method takes a Consumer and tests to see whether the
optional value is present; if it is then the unwrapped value is passed to the consumer

20
Q

What does Optional.ofNullable() do?

A

If you are in a situation in which you might be creating an optional from a null object, you have to use the Optional.ofNullable() method rather than Optional.of() (because the of() method results in a NullpointerException). The ofNullable() creates the optional if the object you pass in is not null.
Otherwise it creates an empty optional.

21
Q

Which methods does the Stream interface provides for searching elements?

A

allMatch, anyMatch, noneMatch, findFirst, and findAny. All of these operations are terminal operations, that is, they all return a single value not a stream.

All of these operations are short-circuiting operations:
that is, as soon as the result is determined then the operation stops

22
Q

What do the methods allMatch(), anyMatch() and noneMatch() return?

A

The methods allMatch(), anyMatch() and noneMatch() all take a Predicate to do a matching test and return a boolean.

23
Q

What does findAny() do?

A

findAny() return any object from the stream after the filters. There is no guarantee it will be the first one, so the output is unpredictable.

24
Q

What is the sorted() method?

A

The stream interface includes a sorted() method that you can use to sort a stream of elements by natural order or by providing a Comparator to determine the order. You can do this in 2 ways:
- Implements the comparable interface on the base class and override the compareTo() where
you describe the way of comparing the objects.
- You can also use sorted(Comparator) in the stream pipeline.
o ducks.stream().sorted((d1, d2) -> d1.getAge() –d2.getAge()).forEach(System.out::println)

25
Q

What are handy static methods for comparators?

A

Comparator has also some handy static methods that you are likely to see when defining
comparators for use with streams: comparing(), reversed(), and thenComparing().

26
Q

What does the distinct function do?

A

This method returns a stream with distinct element so if an element is repeated in the stream, you will end up with only one of them.

27
Q

What is important about streams?

A

Something really important about streams is that you should never, ever try to modify
the source of a stream from within the stream pipeline.

28
Q

What is the collect() method?

A

The collect() is a reduction operation: it reduces a stream into a collection of objects or a value. The Collector you pass to the method specifies how to reduce the stream

29
Q

What is the groupingBy() method?

A

We can use the Collectors.groupingBy() method to group objects by there attribute. For example group people by age. We specify how to group Person object by passing a Function to the groupingBy() method

30
Q

What does the iterate() method?

A
The iterate() creates an infinite sequential Stream, starting with the first argument followed by
elements created by the UnaryOperator that you supply as the second argument
31
Q

How to work safely with infinite streams?

A

To work safely with infinite streams, you need a short-circuiting operation, that could be limit(), or it can also be an operation like findFirst(), findAny(), or anyMatch().