Java 8 Flashcards
What New Features Were Added in Java 8?
Key features include Lambda Expressions, the Stream API, Optional, Method References, Default Methods in Interfaces, and the new Date-Time API.
What Is a Method Reference?
A method reference is a shorthand notation of a lambda expression that executes just one method. It’s denoted by ClassName::methodName.
What Is Optional? How Can It Be Used?
Optional is a container object that may or may not contain a non-null value. It helps avoid NullPointerException and can be used with methods like isPresent(), ifPresent(), or orElse().
Describe some of the Functional Interfaces in the Standard Library.
Some examples are Predicate, Function, Supplier, and Consumer. Functional interfaces are those that have a single abstract method and can be used with lambda expressions.
How are we using immutability in Java 8?
Immutability is promoted with functional programming, such as avoiding side effects in lambda expressions and using final or effectively final variables.
What Is a Functional Interface? What Are the Rules of Defining a Functional Interface?
A functional interface contains exactly one abstract method. You can annotate it with @FunctionalInterface, though this is not mandatory.
What Is a Default Method and When Do We Use It?
A default method is a method in an interface that has an implementation. It allows adding new methods to interfaces without breaking the existing implementations.
What Is a Lambda Expression and What Is It Used for?
Lambda expressions are anonymous functions that simplify the syntax for defining and using methods or instances of functional interfaces.
What Is a Stream? How Does It Differ From a Collection?
A stream is a sequence of data elements that supports various operations. It differs from a collection because it does not store elements, is lazily executed, and focuses on pipeline processing.
What Is the Difference Between Intermediate and Terminal Operations?
Intermediate operations return a new stream (e.g., filter(), map()) and are lazy, while terminal operations (e.g., forEach(), collect()) trigger the stream processing.
What Is the Difference Between Map and flatMap Stream Operation?
map() transforms each element in a stream, while flatMap() flattens multiple levels of nested streams into a single level.
What Is Stream Pipelining in Java 8?
Stream pipelining refers to chaining multiple operations together in a single pipeline, enabling efficient and lazy processing.
Tell Us About the New Date and Time API in Java 8.
Java 8 introduced the java.time package, which provides immutable and thread-safe classes like LocalDate, LocalTime, ZonedDateTime, and more for better handling of dates and times.
What is Type Inference?
Type inference allows the compiler to automatically determine the type of an expression, reducing the need for explicit type declarations.
What is Java 8 StringJoiner class used for?
StringJoiner is used to concatenate strings with a specified delimiter, and optionally, a prefix and suffix.
What is a Spliterator?
Spliterator is an interface introduced in Java 8 to traverse and partition elements of a source like a Stream, Set, or List. It supports parallelism.
Explain the difference between predicate and function.
Predicate<T> is a functional interface that represents a boolean-valued function of one argument, while Function<T, R> represents a function that takes an argument of type T and returns a result of type R.</T>