Stream API Flashcards

1
Q

What does anyMatch() return if the stream is empty?

A

false

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

What does allMatch() return if the stream is empty?

A

true

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

What does noneMatch() return if the stream is empty?

A

true

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

What is the java.util.Optional class?

A

Its a holder for value that can be null

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

How to create Optional objects?

A

Optional empty = Optional.empty();

Optional nonEmptyOptional = Optional.of(“abracadabra”);
(cannot pass null, or else NP)

Optional nullableStr = Optional.ofNullable(null);

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

What does the calculation methods like count(), min(), max() take as argument and what do they return?

A

Take Comparator object as the argument and return an Optional{T}

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

How are the methods groupingBy() and partitioningBy() different?

A

The groupingBy() method takes a classification function (of type Function) and returns the input elements and their matching entries based on the classification function(and organizes the result in a Map{K, List{T}})

The partitioningBy() method takes a Predicate as the argument and classifies the entries as true and false based on the given Predicate(and organizes the results in a Map{Boolean, List{T}})

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

What is the class that can be passed to the .collect() method of stream?

A

Collectors

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

If we want to use the Collectors class to instante a type thats not pre-defined, how can we do it?

A

Collectors.toCollection()

Ex,
Collectors.toCollection(TreeSet::new)

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

How do you you use group by?

A

invoke .collect(Collectors.groupingBy())

ex.

Map{Integer, List{String}} wordGroups = distinctWords.collect(Collectors.groupingBy(String::length));

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

What does Stream -> count() returns?

A

long

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

What does the filter method of IntStream takes?

A

IntPredicate

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

What does

  • flatMapToDouble
  • flatMapToInt
  • flatMapToLong

respectively accept as parameters?

A
  • DoubleStream
  • IntStream
  • LongStream
How well did you know this?
1
Not at all
2
3
4
5
Perfectly