Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why lambda expression in java?

I understand with lambda expression(LE) we can save few lines of coding like object creation for functional interface. Also LE will be more readable. But i am sure that this is not the main reason this feature is provided for. I search it on google for Why Lambda expression in java and i found this interesting quote at this article

Prior to Java 8, processing the elements of any collection could be done by obtaining an iterator from the collection and then iterating over the elements and then processing each element. If the requirement is to process the elements in parallel, it would be done by the client code. With the introduction of Stream API in Java 8, functions can be passed to collection methods and now it is the responsibility of collection to process the elements either in a sequential or parallel manner. -

To me this looks like the advantage of streaming api not LE. Even if LE was not provided developer could have achieved the same with creation of anonymous Consumer class. The only advantage here i can see in favour of LE is developer does not have to remember object of which functional interface needs to be created. So it takes java towards functional programming where dev is saying apply this function and he does not care about object(let jvm handle this).

Is this the main advantage of LE or there is another one major than this ?

Update :-

I tried below simple program where i tried to measure the performance parallel processing with lambda expression. Found that it took 84 unit of time with parallel processing and only 4 unit for sequential. Is it because program is not big enough and also thread overhead may be playing its role here ? May be advantages can be visible in larger function ?

public class Java8Tester {

   final static String salutation = "Hello! ";

   public static void main(String args[]){
      List<String> stringList = new ArrayList<String>();


      for(int i=0;i <100;i++){
      stringList.add("Hello1");
      stringList.add("Hello2");
      }

     long startTime  = System.currentTimeMillis();
       System.out.println(startTime);
      stringList.parallelStream().forEach((string) -> {System.out.println("content is " + string);
      });
      long stopTime  = System.currentTimeMillis();
      System.out.println(stopTime);
      System.out.println("time taken in parallel processing" + (stopTime - startTime)); // took 84 units of time


       startTime  = System.currentTimeMillis();
       System.out.println(startTime);

     for(String str : stringList ) {
         System.out.println("content is " + str);
     }

       stopTime  = System.currentTimeMillis();
      System.out.println(stopTime);
      System.out.println("time taken in sequential processing" + (stopTime - startTime)); // // took 4 units of time
   }


}
like image 495
M Sach Avatar asked Nov 15 '15 10:11

M Sach


People also ask

Why lambda function is used in Java?

Benefits of Lambda Expression In this case, we are not passing any parameter in lambda expression because the run() method of the functional interface (Runnable) takes no argument. Also, the syntax of the lambda expression says that we can omit curly braces ({}) in case of a single statement in the method body.

What is the point of lambda expressions?

Lambda expression is an anonymous function that provides a very concise and functional syntax which is further used for writing anonymous methods. The programming of a function is a body concept, and it's used for creating expression tree types or delegates.

What is the benefit of lambda?

AWS Lambda allows you to integrate your application server needs with mass-mailing services such as SES. Thus, you can consolidate more of the functionality your team requires to operate under one house. This not only reduces administrative costs but also makes your team's workflow more streamlined and efficient.

Why are lambdas expression used in Java 8?

Lambda expressions are used primarily to define inline implementation of a functional interface, i.e., an interface with a single method only. In the above example, we've used various types of lambda expressions to define the operation method of MathOperation interface.


2 Answers

I don't think lambda expressions have one main purpose, rather lots of small added benefits, that together make a huge impact.

  • Lambdas remove a lot of boilerplate code.

  • They make code shorter and easier to read.

  • Reduced need for anonymous inner classes.

  • Allows more code reuse.

Any many more features.

One of my favourite advantages of lambdas is the ability to make default interfaces methods. Something I don't think I can live without now.

So lambdas don't have one specific reason for existing. They open the doors to a lot more functional programming, allow Java to evolve, and overall make coding even more fun.

like image 85
Luke Melaia Avatar answered Sep 28 '22 18:09

Luke Melaia


In general you're right: Stream API could exist without lambda functions, using anonymous classes. Though it would look really ugly and work somewhat less efficiently. As for time measurement, your results are completely irrelevant. See this question for details.

like image 42
Tagir Valeev Avatar answered Sep 28 '22 20:09

Tagir Valeev