assert
keywordenum
@Override
in interfacesLambda Expressions: Lambda expressions allow you to define and use small, inline functions. They are particularly useful when working with functional interfaces. For example:
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
names.forEach(name -> System.out.println(name));
Functional Interfaces: Functional interfaces represent a single abstract method and are the basis for lambda expressions. The java.util.function
package provides many built-in functional interfaces.
Stream API: Streams provide a more functional and concise way to work with sequences of data. For example:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream().filter(n -> n % 2 == 0).mapToInt(Integer::intValue).sum();
Default Methods: Interfaces can have default method implementations, which allows adding new methods to interfaces without breaking existing implementations.
// Define an interface with a default method
interface MyInterface {
// Abstract method
void abstractMethod();
// Default method
default void defaultMethod() {
System.out.println("This is a default method.");
}
}
// Implement the interface in a class
class MyClass implements MyInterface {
@Override
public void abstractMethod() {
System.out.println("Implemented abstractMethod in MyClass");
}
}
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
// Call the abstract method
obj.abstractMethod();
// Call the default method
obj.defaultMethod();
}
}
Method References: Method references allow you to refer to a method by name, simplifying code. For example:
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
names.forEach(System.out::println);
Read more: https://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html
takeWhile
, dropWhile
, and ofNullable
were added to the Stream API to enhance its functionality.