-1

Is there any performance aspect or other strong points to use lambdas or methods references over normal java methods ? How the cost of construction of an object or invoking the methods

I want to know the differences between the traditional and Java 8 Updates

azro
  • 53,056
  • 7
  • 34
  • 70
  • Possible duplicate of [Java lambdas 20 times slower than anonymous classes](https://stackoverflow.com/questions/34585444/java-lambdas-20-times-slower-than-anonymous-classes) – Andrei Makarevich Jan 28 '19 at 11:38
  • Method referencing has actual advantage over lambdas. – Vishwa Ratna Jan 28 '19 at 11:42
  • Check https://stackoverflow.com/questions/1784664/what-is-the-difference-between-declarative-and-imperative-programming – Yogesh Badke Jan 28 '19 at 11:57
  • Possible duplicate of [Lambda vs anonymous inner class performance: reducing the load on the ClassLoader?](https://stackoverflow.com/questions/24764118/lambda-vs-anonymous-inner-class-performance-reducing-the-load-on-the-classloade) – Adesh Kumar Jan 28 '19 at 14:54

4 Answers4

1

There are many advantages of using Java 8 API's over conventional Java methods.

Java 8 basically focus on Functional style(More Declarative, Less Imperative) approach,with Java 8 now we can program in an elegant and fluent, functional style, with higher-order functions. This can lead to concise code that has fewer errors and is easier to understand, maintain, and parallelize.

On top it :

  1. Favor Immutability
  2. Reduce Side Effects
  3. Prefer Expressions Over Statements
  4. lazy execution

There is hell a lot to add, i just gave my 2cents.

Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
1

Java has introduced byte code instruction invokedynamic to construct the anonymous class and then generate byte code for lambdas. So, In case of lambdas java generates the implementation class and generate byte code at runtime. Once the byte code generates the rest of the step is the same as a normal method call. So, in case of lambda/method ref the only overhead is constructing an anonymous class and generate its byte at runtime. So lambdas are bit slower than a normal method call.

Amit Bera
  • 7,075
  • 1
  • 19
  • 42
1

I may assume that the question is about Imperative vs. Declarative programming in Java.

Because lambda/method reference can be compared only to anonymous class performance.

They take different steps before execution:

Lambda      vs    Anonymous Class
linkage           class loading
capture           instantiation
invocation        invocation

Overall benchmarks show that lambda is a bit faster with "hot" benchmarks and significant faster with "cold" benchmarks.

Regarding complex methods and collections processing there is no overall performance benchmarks like X is faster than Y.

You should specify a case to compare performance benchmarks, in some cases classic approach can work faster, in some case - functional. I may assume that in most cases classic approach will work faster, especially when working with mutable objects.

But it's much easier to process a collection in a parallel way with functional style. Streams are lazily-executed, what its an advantage.

It's very good feature that Java supports functional programming, streams and lambdas, but they are not silver bullet. Use them wisely.

J-Alex
  • 6,881
  • 10
  • 46
  • 64
0

There are several write-ups you can google, but they all seem to show that Lambdas are not as performant as for loops and anonymous classes. I don't know if Java 9 or Java 10 improved the performance of lambdas at all, but I couldn't find anything that seemed to suggest they did.

I think the main benefit for lambdas is read-ability. That said, I have seen some lambda code that was not very readable at all, so I guess it's like most things where you can abuse it.

CodeChimp
  • 8,016
  • 5
  • 41
  • 79
  • do we need consumer or supplier over normal java methods – shyam sanju Jan 28 '19 at 12:10
  • To use a lambda, no. For instance I can do `myList.stream().map(item -> item.toString).collect(Collectors.toList()) to convert `myList` to a list of `String`s using `toString()`. But if you were writing your own lambda I believe you have to use `Consumer` or one of it's variants (there's like a BinaryConsumer and some others???). I haven't done the latter much so I am not 100% sure. – CodeChimp Jan 28 '19 at 15:49