0 votes
in Java by
Java 8 features with examples. Describe java 8 new features with running example.

1 Answer

0 votes
by (2.8k points)
There are following new feature added in java 8:-
1. Interface Default and Static Methods:-
Java 8, interfaces are enhanced the method to use default and static keyword to create interfaces with method implementation.
For Example:-
public interface Interface1 {
    void method1(String str);
    default void log(String str){
        System.out.println("I1 logging::"+str);
    static void print(String str){
        System.out.println("Printing "+str);

2. ForEach() method:-Java 8 provides a new method called forEach() to iterate over collection framework.
    Map<Integer, String> myMap = new HashMap<>();
    myMap.put(1, "Delhi");
        myMap.put(2, "Washington");
        myMap.put(3, "Tokyo");
        myMap.put(4, "Ottawa");
    //Lambda Expressions
    myMap.forEach((k,v)->System.out.println("Key: "+k+" : Value: "+v));
            // Write our business logic here..

3. Lambda Expressions: - Lambda Expressions added in java 8. It enables you to treat functionality as a method argument or code as data.

    // Creating a thread in old way
        new Thread(new Runnable(){
            public void run(){
                System.out.println("In run method in old programming way");
        // Creating a thread using lambda expression.
        new Thread(() -> System.out.println("In run method using lambda expression")).start();

4. Date and Time API:- Before java 8 classes java.util.Date and SimpleDateFormatter are not thread-safe. In Java 8, the date and time API in java.time package that offers greatly improved safety and functionality for developers.
5. Collection API improvements:- Performance Improvement for HashMap class with Key Collisions. Map replaceAll(), compute(), merge() methods.

Share:- Whatsapp Facebook Facebook

Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.