Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

 Lambda Expression In Java:-

..................................................................................................................................................................

What is a lambda expression?

It is a new feature of Java 8 that is used to define the method in a concise pattern. Lambda function uses an anonymous method to block to define functionality.

if we want to implement the features under the method of interface or abstract class then we can use a lambda expression.

Syntax of a lambda expression?

()->{

Expression


}

1) Example of Lambda Expression without return type method

package com.scs;

interface A

{

    public void fun();

}

interface B

{

    public  int fun1();

}


public class LamdaExpression {

    public static void main(String[] args) {

        A obj = () -> {

                System.out.println("Lambda Expression A");

        };

       B obj1 = () -> {

            System.out.print("Lambda Expression B");

       };

        obj.fun();

        obj1.fun1();



    }

}



2) Lambda Expression Example With Return Type:-

package com.scs;

interface A

{

    public int fun();

}

public class LamdaExpression {

    public static void main(String[] args) {

        A obj = () -> {

               return 100;

        };


        System.out.println(obj.fun());

      }

}



3) Lambda Expression With Single Parameters?


package com.scs;

interface A

{

    public int fun(String x);

}


public class LamdaExpression {

    public static void main(String[] args) {

        A obj = (x) -> {

               return 100;

        };

System.out.println(obj.fun("Hello"));




    }

}



4) Lambda Expression With Multiple Parameters?


package com.scs;


interface A

{

    public int fun(int x, int y);

}




public class LamdaExpression {

    public static void main(String[] args) {

        A obj = (x,y) -> {

               return x+y;

        };


        System.out.println(obj.fun(100,20));




    }

}



5) Assign Parameters Under Lambda Function:-


    package com.scs;


    interface A

    {

        public int fun(int x, int y);

    }




    public class LamdaExpression {

        public static void main(String[] args) {

            A obj = (int x,int y) -> {

                   return x+y;

            };


            System.out.println(obj.fun(100,20));




        }

    }



What is forEach method?


it is similar to for each loop in java but it will be defined as a lambda expression method.

It is used to display the elements of collection objects.


Example of forEach method in Java:-


List <ref> = new ArrayList();


<ref>.foreach(

item->System.out.println(item)

);


Example to ArrayList object using forEach() in Java?


package com.scs;


import java.util.ArrayList;


public class ForeachMethod {

    public static void main(String[] args) {

        String arr[] = {"C","CPP","DS","JAVA","PHP",".NET","iOS"};

        ArrayList obj = new ArrayList();

        for(Object o:arr)

        {

            obj.add(o);

        }

     /*   obj.add("C");

        obj.add("CPP");

        obj.add("DS");

        obj.add("Java");*/


        obj.forEach(

                item -> System.out.println(item)


        );

    }


}



Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post