Here is a comprehensive list of new features introduced in Java 8:
- Lambda Expressions
- Example:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.forEach(n -> System.out.println(n));
- Stream API
- Example:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream().reduce(0, (a, b) -> a + b);
System.out.println("Sum: " + sum);
- Method References
- Example:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.sort(Comparator.naturalOrder());
- Default and Static Methods in Interfaces
- Example:
interface Shape {
double area();
default void printArea(Shape shape) {
System.out.println("Area: " + shape.area());
}
}
- Date and Time API
- Example:
LocalDate today = LocalDate.now();
System.out.println("Today's date: " + today);
- Optional Class
- Example:
Optional<String> optional = Optional.of("value");
if (optional.isPresent()) {
System.out.println("Value: " + optional.get());
}
- Base64 Encoding and Decoding
- Example:
String text = "Hello, World!";
String encoded = Base64.getEncoder().encodeToString(text.getBytes());
System.out.println("Encoded: " + encoded);
byte[] decoded = Base64.getDecoder().decode(encoded);
System.out.println("Decoded: " + new String(decoded));
- Parallel Array Sorting
- Example:
int[] numbers = {5, 4, 3, 2, 1};
Arrays.parallelSort(numbers);
System.out.println(Arrays.toString(numbers));
- Functional Interfaces
- Example:
Function<Integer, Integer> square = n -> n * n;
System.out.println("Square: " + square.apply(5));
- Nashorn JavaScript Engine
- Example:
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval("print('Hello, Nashorn!')");
These are all the new features introduced in Java 8, along with brief examples to demonstrate how they work.
No comments:
Post a Comment