there are alot of new feature have been added in java 8. below are few summarising:
Features introduced in Java 8, along with a brief description, syntax and example
Feature | Description | Syntax | Example (Car) |
---|---|---|---|
Lambda Expressions | A way to pass around behavior as if it were data. | (parameters) -> expression | (Car car) -> car.getSpeed() |
Method Reference | A shorthand notation for a lambda expression that just calls an existing method. | ClassName::methodName | Car::getSpeed |
Functional Interfaces | Interfaces with exactly one abstract method that can be used as the type of a lambda expression. | interface SomeName {…} | interface CarSpeedChecker { boolean test(Car car); } |
Stream API | A powerful and efficient way to process data. | stream.method() | cars.stream().filter(c -> c.getSpeed() > 50).count() |
Default Methods | A way to add new functionality to existing interfaces without breaking existing implementations. | default methodName() {…} | default int getMaxSpeed() { return 150; } |
Optional | A container object used to represent the absence of a value. | Optional.of(value) | Optional<Car> optCar = Optional.ofNullable(car); |
Date and Time API | A comprehensive new API for working with dates and times. | LocalDate.now() | LocalDate manufactureDate = car.getManufactureDate(); |
java code with all java 8 feature in one example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
import java.util.ArrayList; import java.util.List; import java.util.function.Predicate; import java.util.stream.Collectors; public class CarExample { public static void main(String[] args) { List<Car> cars = new ArrayList<>(); cars.add(new Car("Toyota", "Camry", 2018, 22000.00)); cars.add(new Car("Honda", "Civic", 2020, 25000.00)); cars.add(new Car("Tesla", "Model S", 2022, 100000.00)); // Lambda expressions cars.forEach(c -> System.out.println(c.toString())); // Method references cars.forEach(System.out::println); // Stream API double avgPrice = cars.stream().mapToDouble(c -> c.getPrice()).average().getAsDouble(); System.out.println("Average Price: " + avgPrice); // Filter with Predicate Predicate<Car> priceLessThan30000 = c -> c.getPrice() < 30000; List<Car> affordableCars = cars.stream().filter(priceLessThan30000).collect(Collectors.toList()); System.out.println("Affordable Cars: " + affordableCars); } static class Car { private String make; private String model; private int year; private double price; public Car(String make, String model, int year, double price) { this.make = make; this.model = model; this.year = year; this.price = price; } public String getMake() { return make; } public String getModel() { return model; } public int getYear() { return year; } public double getPrice() { return price; } @Override public String toString() { return make + " " + model + " " + year + " $" + price; } } } |
In above example, we first created a List
of Car
objects and added some car instances to the list.
Then, we used a forEach
loop with a lambda expression to print out each car. We also used method references to achieve the same result.
Next, we used the Stream API to calculate the average price of the cars in the list. We also filtered the list of cars using a Predicate
to get a list of affordable cars (price less than $30,000).