Java 8 Features
1. Lambda Expressions
Lambda expressions enable concise syntax for writing functional interfaces. They allow you to treat functionality as a method argument, creating more readable and maintainable code.
List<String> names = Arrays.asList("Shivaji", "Chandra", "Gupta");
names.forEach(name -> System.out.println(name));
2. Streams
Streams provide a new abstraction for working with sequences of elements. They allow for concise and expressive operations on data such as filtering, mapping, and reducing.
List<String> names = Arrays.asList("Shivaji", "Chandra", "Gupta");
long count = names.stream().filter(name -> name.startsWith("J")).count();
System.out.println("Count: " + count);
3. Functional Interfaces
Functional interfaces have exactly one abstract method and can be used as the basis for lambda expressions. They provide a way to represent single-method interfaces more compactly.
@FunctionalInterface
interface MyFunctionalInterface {
void myMethod();
}
4. Default Methods
Default methods allow the addition of new methods to interfaces without breaking existing implementations. They provide a default implementation that can be overridden by implementing classes.
interface MyInterface {
void myMethod();
default void defaultMethod() {
System.out.println("Default Method");
}
}