Skip to main content

java 8 features

 

1. Lambda Expressions

A lambda expression allows you to write concise code by passing behavior as a parameter.

Example:



import java.util.Arrays; import java.util.List; public class LambdaExample { public static void main(String[] args) { List<String> names = Arrays.asList("Alice", "Bob", "Charlie"); // Using lambda expression names.forEach(name -> System.out.println(name)); } }


2. Stream API

The Stream API provides a functional approach to process collections of data.

Example:

import java.util.Arrays; import java.util.List; public class StreamExample { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); // Filtering and mapping with Stream API numbers.stream() .filter(n -> n % 2 == 0) // Filters even numbers .map(n -> n * n) // Squares the numbers .forEach(System.out::println); } }

3. Default Methods in Interfaces

Java 8 allows you to add default implementations to methods in interfaces.

Example:

interface Vehicle { default void start() { System.out.println("Vehicle is starting..."); } } class Car implements Vehicle { } public class DefaultMethodExample { public static void main(String[] args) { Car car = new Car(); car.start(); // Calls the default method } }

4. Functional Interfaces and @FunctionalInterface Annotation

A functional interface has exactly one abstract method and can be represented with

a lambda expression.

Example:

@FunctionalInterface interface Calculator { int operate(int a, int b); } public class FunctionalInterfaceExample { public static void main(String[] args) { Calculator addition = (a, b) -> a + b; System.out.println("Addition: " + addition.operate(5, 3)); } }

5. Optional Class

The Optional class is used to handle null values gracefully.

Example:

import java.util.Optional; public class OptionalExample { public static void main(String[] args) { Optional<String> name = Optional.ofNullable("John"); name.ifPresentOrElse( System.out::println, () -> System.out.println("Name is not present") ); } }

6. Method References

Method references are a shorthand for referring to methods or constructors using ::.

Example:

import java.util.Arrays; import java.util.List; public class MethodReferenceExample { public static void main(String[] args) { List<String> names = Arrays.asList("Alice", "Bob", "Charlie"); // Method reference to print each name names.forEach(System.out::println); } }

7. Date and Time API

Java 8 introduced a new date and time API under the java.time package.

Example:

import java.time.LocalDate; import java.time.LocalTime; import java.time.LocalDateTime; public class DateTimeExample { public static void main(String[] args) { LocalDate date = LocalDate.now(); LocalTime time = LocalTime.now(); LocalDateTime dateTime = LocalDateTime.now(); System.out.println("Date: " + date); System.out.println("Time: " + time); System.out.println("DateTime: " + dateTime); } }

8. Collectors in Stream API

Collectors provides utility methods for processing streams.

Example:


import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public class CollectorsExample { public static void main(String[] args) { List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "Bob"); // Collect unique names into a list List<String> uniqueNames = names.stream() .distinct() .collect(Collectors.toList()); System.out.println(uniqueNames); }
}

Comments

Popular posts from this blog

equals method of object class in java

  In Java, the equals method is a fundamental method inherited by all classes from the Object class.           Here’s an explanation of the equals method: Purpose: The equals method is used to compare two objects to determine if they are logically equivalent. By default, the implementation in the Object class checks if the two object references point to the same memory location (i.e., if they are the same instance). Signature : public boolean equals(Object obj) Explanation: Method Override: Classes can override the equals method to provide their own definition of object equality based on their specific needs. When overriding, the method should adhere to certain principles to ensure consistency and correctness of equality comparisons. Default Behavior (from Object class): The default implementation in the Object class checks if the two object references ( this and obj ) refer to the exact same object in memory using the == operator: public boo...

How to convert Enum Contants into String: Inbuilt methods of Enum in java

 In Java, every enum type inherits several built-in methods from the java.lang.Enum class, which is the base class for all enum types. These built-in methods provide functionality such as getting the name of an enum constant, comparing enum constants, and iterating over all constants. Here are the inbuilt methods provided by the Enum class: 1. values() Description : This method returns an array of all the constants of the enum type, in the order they were declared. Syntax : public static T[] values() Example : enum Day {     MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY; } public class EnumMethodsExample {     public static void main(String[] args) {         Day[] days = Day.values();  // Get all enum constants                  // Loop through the array and print each day         for (Day day : days) {             System.out....

What is real use of interface: mostly asked interview question

Actually in real project development interfaces are use to write business logic code . Also Interfaces in java are powerful tools that allow you to define a contract for what a class can do, without specifying how it does it. it means we can give method declaration in interface and what that method does actually that responsibility is given to the class which is going to implement that class. They are used for several real-world purposes like enabling polymorphism , creating loosely coupled systems , and defining common behaviors across different classes . Real-World Use of Interfaces Let's look at some practical scenarios where interfaces are commonly used: 1. Multiple Inheritance (via Interfaces):  Java doesn't support multiple inheritance with classes, but it allows multiple inheritance with interfaces. This allows a class to implement multiple interfaces, enabling it to inherit behaviors from more than one source. 2. Polymorphism: Interfaces allow you to treat different o...