Idiomatic Java 8: Lambdas and Streams Flashcards

1
Q

How is Java 8 different from the other versions as described by the author?

A

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.

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

What are some of the new features of Java 8?

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

What are lambda expressions?

A

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.

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

What is the syntax for lambda expressions?

A

The syntax for lambda expressions is parameter -> body.

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

What are method references?

A

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.

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

What are functional interfaces (added in Java 8)?

A

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

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

What are several new functional interfaces that Java 8 comes with in the package java.util.function?

A
  • Function<T, R>
  • Supplier<T></T>
  • Predicate<T></T>
  • Consumer<T></T>
  • BiFunction
  • BiConsumer
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the Stream interface?

A

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.

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

What are the key advantages of the Stream interface?

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

What is parallelStream() in Java?

A

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.

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

What does the peek() in a Java stream?

A

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.

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

What does the limit(int n) operation do in Java stream?

A

The limit(int n) method can be used to restrict a stream to the given number of elements.

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

What are collectors in Java?

A

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.

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

What do more complex collectors resolve to in Java?

A

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.

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

What do the groupingBy and partitioningBy collectors do?

A

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…])

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

What is the Optional class (added in Java 8)?

A

The Optional class is a container class that is used to represent an optional value. It is designed to handle situations where a value may or may not be present, preventing the need for explicit null checks and reducing the chances of null pointer exceptions.

17
Q

How can we run JavaScript in Java?

A

We can run JavaScript in Java by importing the ScriptEngine and ScriptEngineManager from 'javax.script. Then we create a new engineManager and get the engine we want to run JavaScript.