Stream API Flashcards
What does anyMatch() return if the stream is empty?
false
What does allMatch() return if the stream is empty?
true
What does noneMatch() return if the stream is empty?
true
What is the java.util.Optional class?
Its a holder for value that can be null
How to create Optional objects?
Optional empty = Optional.empty();
Optional nonEmptyOptional = Optional.of(“abracadabra”);
(cannot pass null, or else NP)
Optional nullableStr = Optional.ofNullable(null);
What does the calculation methods like count(), min(), max() take as argument and what do they return?
Take Comparator object as the argument and return an Optional{T}
How are the methods groupingBy() and partitioningBy() different?
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}})
What is the class that can be passed to the .collect() method of stream?
Collectors
If we want to use the Collectors class to instante a type thats not pre-defined, how can we do it?
Collectors.toCollection()
Ex,
Collectors.toCollection(TreeSet::new)
How do you you use group by?
invoke .collect(Collectors.groupingBy())
ex.
Map{Integer, List{String}} wordGroups = distinctWords.collect(Collectors.groupingBy(String::length));
What does Stream -> count() returns?
long
What does the filter method of IntStream takes?
IntPredicate
What does
- flatMapToDouble
- flatMapToInt
- flatMapToLong
respectively accept as parameters?
- DoubleStream
- IntStream
- LongStream