Postingan

Menampilkan postingan dengan label Lambda expression

What is a Functional interface in Java 8? @FunctionalInterface Annotation Examples Tutorial

Gambar
The Functional interface is one of the most important concepts of Java 8 which actually powers lambda expression but many developers don't put enough effort to understand it and spend time learning lambda expression and Stream API without first understanding the role of the functional interface in Java 8. Unless you know what is a functional interface and how lambda is related to it, you can't use powerful features of Java 8 likeLambda expression and Stream API. Without knowledge of functional interface , you won't be able to understand where you can use a lambda in the code but also you will struggle to write lambda expression the method is expecting, hence, it's important to have a good understanding of functional interface in Java 8. In this article, I'll try to fill that gap by explaining what is a functional interface, what is a @FunctionalInterface annotation, how they are related to a lambda expression, and how they help you to use the lambda expression in ...

How to Convert a Lambda Expression to Method Reference in Java 8?

Gambar
If you have been coding in Java 8 then you may know that using method reference in place of lambda expression makes your code more readable, hence it is advised to replace lambda expression with method reference wherever possible. But, the big question is, how do you find whether you can replace a lambda with method reference? Yes, it's not that easy, especially if you have been using Java 8 only for a couple of months and struggling to get the functional programming concepts and idioms sorted in your head. Sometimes, IDEs like IntelliJ IDEA and Eclipse does offer some hints to convert lambda expression to method reference but it does make sense to learn the logic behind it, otherwise, it won't make sense. The simple rule to replace lambda expression with method reference is built on common sense, which you will learn in this article. If you look closely, lambda is nothing but a code block which you pass to a function to execute. If you already have that code in form of a meth...