Java 8
Java 8
**Answer:**
- **Lambda Expressions:** Allowing for more concise code and functional programming styles.
- **Streams API:** Enabling functional-style operations on collections for easier data manipulation.
- **Default and Static Methods in Interfaces:** Allowing interfaces to have method implementations.
- **Date and Time API:** A new date and time handling framework that is more comprehensive and
user-friendly.
**Answer:**
A lambda expression is a concise way to represent a functional interface using an expression. It consists
of parameters, the arrow operator (`->`), and a body. For example:
```java
```
This expression takes two integers and returns their sum. Lambda expressions are often used with the
Streams API and functional interfaces.
**Answer:**
The Streams API allows for functional-style operations on streams of data. It enables operations like
filter, map, reduce, and collect, making it easier to process collections in a more readable and efficient
manner. Streams can be processed in parallel to take advantage of multi-core processors.
**Answer:**
The `Optional` class is a container object that may or may not contain a non-null value. It helps in
avoiding `NullPointerExceptions`. For example:
```java
optional.ifPresent(System.out::println);
```
**Answer:**
Default methods allow developers to add new methods to interfaces without breaking existing
implementations. They are defined using the `default` keyword:
```java
interface MyInterface {
System.out.println("Default implementation");
```
This feature enables backward compatibility while still allowing interfaces to evolve.
### 6. How does the new Date and Time API differ from the old Date API?
**Answer:**
The new Date and Time API introduced in Java 8 (in the `java.time` package) is more comprehensive and
user-friendly compared to the old `java.util.Date` and `java.util.Calendar` classes. Key differences
include:
- **Better Design:** It separates date and time, supports time zones, and provides a more fluent API for
operations.
- **Human-Readable:** It provides clear methods for parsing, formatting, and calculating dates and
times.
**Answer:**
```java
@FunctionalInterface
interface MyFunctionalInterface {
void execute();
```
### 8. Can you explain the concept of method references?
**Answer:**
Method references provide a shorthand notation for referring to methods of existing classes or objects.
They enhance code readability by allowing you to reference methods directly rather than using lambda
expressions. There are four types of method references:
Example:
```java
```
### 9. How can you filter a collection using the Streams API?
**Answer:**
You can filter a collection using the `filter` method provided by the Streams API. For example:
```java
.collect(Collectors.toList());
```
This code filters names that start with "J" and collects them into a new list.
### 10. What is the difference between `map` and `flatMap` in the Streams API?
**Answer:**
The `map` method transforms each element of the stream into another object, while `flatMap` is used
when each element of the stream can be mapped to zero or more elements, effectively flattening the
resulting stream.
Example:
```java
.flatMap(List::stream)
```
Feel free to ask if you need more questions or deeper explanations on specific topics!