Idiomatic Java 8: Lambdas and Streams Flashcards
How is Java 8 different from the other versions as described by the author?
The author describes that as of publication, Java 8 is the de-facto standard version of Java that is already being used in many production systems.
What are some of the new features of Java 8?
- Lambda expressions
- Method references
- Default methods (Defender methods)
- A new stream API
- Optional
- A new Date/Time API
- Nashorn, the new JavaScript engine
- Removal of the Permanent Generation
What are lambda expressions?
Lambda expressions are like syntactic sugar for a class with one method whose type is inferred.
This was one of the biggest new features of Java 8 and has huge implications for simplifying development.
What is the syntax for lambda expressions?
The syntax for lambda expressions is parameter -> body
.
What are method references?
In Java, method references provided a concise way to refer to methods or constructors without invoking them directly.
They act as a shorthand notation for lambda expressions when the lambda expression simply calls an existing method or constructor.
What are functional interfaces (added in Java 8)?
A functional interface is defined as an interface with exactly one abstract method.
Any interface can be a functional interface. To declare our intention that an interface is functional, use the @FunctionalInterface annotation. Although not necessary, it will cause a compilation error if our interface does not satisfy the requirements (such as one abstract method).
What are several new functional interfaces that Java 8 comes with in the package java.util.function?
- Function<T, R>
- Supplier<T></T>
- Predicate<T></T>
- Consumer<T></T>
- BiFunction
- BiConsumer
What is the Stream
interface?
The Stream
interface is a sequence of elements that can be processed in a declarative and functional way. It represents a pipeline of data that allows you to perform operations on data such as filtering, mapping, sorting, and reducing, in a concise and expressive manner.
It is located in the java.util.stream
package.
What are the key advantages of the Stream
interface?
- Data Processing Pipeline: subsequent operation to process data in a pipeline manner
- Laziness and Short-circuiting: Operations are not executed immediately, only if needed.
- Functional Operations: Filtering, sorting, mapping, reducing, and more.
- Parallel Processing: Parallelism to execute operations concurrently
- Integration with Collections and Arrays: Seamlessly integrates with existing data structures
What is parallelStream()
in Java?
parallelStream()
allows subsequent operations in a stream pipeline to be performed in parallel as soon as a segment of data is processed by preceding operations instead of having to wait for the entire operation to complete.
For instance, if we want to list the GPA of only those students who are seniors, sequential processing would have to wait for the filtering of all students who are seniors and then start listing them.
However, with parallel processing, as soon as each senior is filtered, it can send the listing of that senior to another processor, decreasing the compute time.
What does the peek()
in a Java stream?
In Java, peek()
is an intermediate operation that allows you to perform a specific action on each element of the stream while maintaining the stream pipeline.
It does not modify the stream or its elements and returns a new stream with the same elements as the original stream.
What does the limit(int n)
operation do in Java stream?
The limit(int n) method can be used to restrict a stream to the given number of elements.
What are collectors in Java?
In Java, a Collector
represents a way to combine the elements of a Stream
into one result. Since streams are lazily evaluated and support parallel execution, Collector
provides a special way to combine results.
What do more complex collectors resolve to in Java?
More complex collectors resolve to a single value. For instance, we can use an averaging
Collector to get the average.
These along with SummaryStatistics
(for instance java.util.InSummaryStatistics
) are useful for calculating statistic of a data.
What do the groupingBy
and partitioningBy
collectors do?
The groupingBy
collector groups elements based on a function we provide.
Similarly, the partitioningBy
method creates a map with a boolean key. (Result: {false:[elements…], true=[elements…])