Lambda expressions enable functional programming paradigms in Java, simplifying syntax for anonymous class implementations. They are particularly useful for event listeners and collection operations.
// Anonymous inner class implementation
new Thread(new Runnable() {
public void run() {
System.out.println("Legacy thread execution");
}
}).start();
// Lambda equivalent
new Thread(() -> System.out.println("Lambda thread execution")).start();
Functional Interfaces
Interfaces with a single abstract method can be implemented using lambda expressions. Java 8 provides common functioanl interfaces in the java.util.function package.
@FunctionalInterface
interface SimpleOperation {
void execute();
}
SimpleOperation op = () -> System.out.println("Operation performed");
op.execute();
Method and Constructor References
These provide shorthand syntax for lambda expressions that call existing methods or construct new objects.
List<String> items = Arrays.asList("item1", "item2", "item3");
items.forEach(System.out::println);
Supplier<ArrayList<String>> listCreator = ArrayList::new;
ArrayList<String> newList = listCreator.get();
Stream Processing API
The Stream API enables declarative processing of collections through pipelined operations with intermediate and terminal processing stages.
List<String> words = Arrays.asList("apple", "banana", "avocado");
List<String> result = words.stream()
.filter(word -> word.startsWith("a"))
.collect(Collectors.toList());
Interface Default and Static Methods
Interfaces can now include concrete method implementations using the default keyword, along with static utility methods.
interface EnhancedInterface {
default void standardAction() {
System.out.println("Default implementation");
}
static void utilityMethod() {
System.out.println("Static helper method");
}
}
class Implementation implements EnhancedInterface {
// Inherits default method
}
Date and Time API
The java.time package introduces immutable types for date and time manipulation, replacing error-prone legacy classes.
LocalDate currentDate = LocalDate.now();
LocalDate birthDate = LocalDate.of(1995, Month.JULY, 15);
Duration ageDifference = Duration.between(birthDate.atStartOfDay(), currentDate.atStartOfDay());
Optional Container Type
The Optional class provides a type-safe approach to handle potential null values without explicit null checks.
Optional<String> textContainer = Optional.ofNullable(getText());
textContainer.ifPresent(content -> System.out.println("Content: " + content));
Nashorn JavaScript Engine
Java 8 integrates the Nashorn engine for executing JavaScript code within the JVM environment.
ScriptEngine jsEngine = new ScriptEngineManager().getEngineByName("nashorn");
jsEngine.eval("print('JavaScript executed in JVM');");
Base64 Encoding Utilities
Native Base64 encoding and decoding support eliminates the need for externla libraries.
String original = "Data to encode";
String encoded = Base64.getEncoder().encodeToString(original.getBytes());
byte[] decoded = Base64.getDecoder().decode(encoded);