Junior — Middle
Have you written code following the functional programming style?
sobes.tech AI
Answer from AI
The functional programming style in Java involves using functions as first-class objects, absence of side effects, and immutability of data. With the advent of Java 8, lambda expressions and the Stream API appeared, allowing code to be written in a functional style.
Example of using the functional style for filtering and transforming a list:
import java.util.*;
import java.util.stream.*;
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]
This approach improves readability and allows for writing more declarative code.