Junior — Middle
Do you have practical experience using features and new capabilities introduced in Java 8?
sobes.tech AI
Answer from AI
Yes, Java 8 introduced important new features that significantly simplified and improved the code:
-
Lambda expressions — allow writing compact anonymous functions, making it easier to work with functional interfaces.
-
Stream API — provides a convenient way to process collections of data in a functional style (filtering, transforming, aggregating).
-
Default methods in interfaces — enable adding method implementations in interfaces without breaking existing code.
-
Optional — a class for handling the possible absence of a value, helping to avoid
NullPointerException.
Example of using Stream and lambda:
import java.util.*;
import java.util.stream.*;
public class Example {
public static void main(String[] args) {
List<String> names = Arrays.asList("Anna", "Bob", "Charlie", "David");
List<String> filtered = names.stream()
.filter(name -> name.startsWith("A"))
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println(filtered); // [ANNA]
}
}