Java 8 new features

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

FeatureDescriptionSyntaxExample (Car)
Lambda ExpressionsA way to pass around behavior as if it were data.(parameters) -> expression(Car car) -> car.getSpeed()
Method ReferenceA shorthand notation for a lambda expression that just calls an existing method.ClassName::methodNameCar::getSpeed
Functional InterfacesInterfaces 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 APIA powerful and efficient way to process data.stream.method()cars.stream().filter(c -> c.getSpeed() > 50).count()
Default MethodsA way to add new functionality to existing interfaces without breaking existing implementations.default methodName() {…}default int getMaxSpeed() { return 150; }
OptionalA container object used to represent the absence of a value.Optional.of(value)Optional<Car> optCar = Optional.ofNullable(car);
Date and Time APIA 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:

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).

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top