1/21
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Q: What is a Lambda Expression in Java?
A Lambda Expression is a concise way to represent an anonymous function (or method) that can be passed around as an argument or assigned to variables.
Q: How do you write a Lambda Expression in Java?
A Lambda Expression consists of parameters (optional), an arrow token (->
), and a body. Example: (a, b) -> a + b
.
Q: What is the advantage of using Lambda Expressions in Java?
Lambda Expressions simplify code, making it more readable and concise. They allow for functional-style programming and make working with collections easier.
Q: What are the types of parameters in Lambda Expressions?
Lambda Expressions can take multiple parameters, but they must be of the same type. You can also use an empty parameter list when there are no parameters.
Q: What is a Functional Interface in Java?
A Functional Interface is an interface with exactly one abstract method. They can have multiple default or static methods but must have only one abstract method.
Q: Why are Functional Interfaces important in Java 8?
Functional Interfaces are used to define the target type for Lambda Expressions and method references. They allow functional programming features to be used in Java.
Q: Can a Functional Interface have multiple abstract methods in Java?
No, a Functional Interface can have only one abstract method. If it has more than one abstract method, it is no longer considered a Functional Interface.
Q: How do you declare a Functional Interface in Java?
A Functional Interface is declared using the @FunctionalInterface
annotation (optional but recommended) to indicate that it has exactly one abstract method.
Q: What is the purpose of the @FunctionalInterface
annotation?
The @FunctionalInterface
annotation is used to mark an interface as a functional interface, ensuring that it has exactly one abstract method. It helps the compiler detect errors.
Q: What is the Stream API in Java 8?
The Stream API is a new abstraction for working with sequences of elements in a functional style. It allows operations like filtering, mapping, and reducing to be performed on collections.
Q: How do you create a Stream in Java 8?
A Stream can be created from a collection (e.g., List
, Set
) using the stream()
method. Example: list.stream()
.
Q: What is the benefit of using the Stream API in Java?
The Stream API allows for more expressive, readable, and declarative code. It promotes functional programming by allowing operations on data in a pipeline style.
Q: What is the difference between a regular loop and the Stream API?
While regular loops process elements imperatively, the Stream API processes elements in a declarative, functional style, making the code more concise and easier to read.
Q: What are some common operations available in the Stream API?
Common operations in the Stream API include filter()
, map()
, collect()
, reduce()
, forEach()
, sorted()
, etc., for processing data in a pipeline.
Q: What does the filter()
method do in the Stream API?
The filter()
method is used to filter elements from a stream based on a given condition or predicate.
Q: What does the map()
method do in the Stream API?
The map()
method is used to transform elements in a stream by applying a function to each element, producing a new stream.
Q: What is the reduce()
method in the Stream API?
The reduce()
method is used to combine the elements of a stream into a single result (e.g., summing, multiplying, concatenating).
Q: Can a Stream be reused in Java?
No, once a stream has been consumed (e.g., by calling a terminal operation like collect()
or forEach()
), it cannot be reused. You need to create a new stream.
Q: What is a terminal operation in the Stream API?
A terminal operation is an operation that consumes the stream and produces a result (e.g., collect()
, reduce()
, forEach()
, count()
). After a terminal operation, the stream cannot be reused.
Q: What is a lazy operation in the Stream API?
Lazy operations are intermediate operations (like filter()
and map()
) that do not perform any computation until a terminal operation is invoked. They are designed to improve performance.
Q: How does the Stream API support parallel processing?
The Stream API supports parallel processing with the parallelStream()
method, which allows for multi-core processors to execute operations concurrently for better performance.