Raja has Published 469 Articles

What are the rules for the body of lambda expression in Java?

raja

raja

Updated on 11-Jul-2020 12:48:49

1K+ Views

A lambda expression is an anonymous function (nameless function) that has passed as an argument to another function. We need to follow some rules while using the body of a lambda expression.Rules for the body of a lambda expressionThe body of the lambda expression can be either a single expression or more statements.If we are using ... Read More

Why we use lambda expressions in Java?

raja

raja

Updated on 11-Jul-2020 12:43:42

4K+ Views

A lambda expression can implement a functional interface by defining an anonymous function that can be passed as an argument to some method.Enables functional programming: All new JVM based languages take advantage of the functional paradigm in their applications, but programmers forced to work with Object-Oriented Programming (OOPS) till lambda expressions came. Hence lambda ... Read More

How to sort a collection by using Stream API with lambdas in Java?

raja

raja

Updated on 11-Jul-2020 12:43:12

525 Views

A Stream API is a powerful way to achieve functional programming in Java. It usually works in conjunction with a lambda expression and provides an efficient way to perform data manipulation operations like sort, filter, map, reduce and etc.In the below example, we can sort a collection using Stream API. It provides sorting logic by using the sorted() method ... Read More

Importance of Predicate interface in lambda expression in Java?

raja

raja

Updated on 11-Jul-2020 12:38:14

2K+ Views

Predicate is a generic functional interface that represents a single argument function that returns a boolean value (true or false). This interface available in java.util.function package and contains a test(T t) method that evaluates the predicate of a given argument.Syntaxpublic interface Predicate {  boolean test(T t); }Exampleimport java.util.*; import java.util.functionPredicate; public class LambdaPredicateTest {   ... Read More

How to find a maximum value in a collection using method reference in Java?

raja

raja

Updated on 11-Jul-2020 12:29:58

223 Views

A method reference provides a way in the lambda expression to refer a method without executing it. It requires a target type context that consists of a compatible functional interface.Syntax :: In the below example, we can find out the maximum value of an ArrayList using method reference.Exampleimport java.util.*; class MyClass { ... Read More

How to use method references with Generics in Java?

raja

raja

Updated on 11-Jul-2020 12:25:52

954 Views

The method references are introduced in Java 8 similar to lambda expression. It can allow us to reference methods or constructors without executing them. The method references and lambda expressions require a target type that consists of a compatible functional interface. We can also use a method reference with generic classes and generic methods in java.Exampleinterface MyFunc {    int func(T[] vals, ... Read More

What is the generic functional interface in Java?

raja

raja

Updated on 11-Jul-2020 12:23:09

4K+ Views

A lambda expression can't specify type parameters, so it's not generic. However, a functional interface associated with lambda expression is generic. In this case, the target type of lambda expression has determined by the type of argument(s) specified when a functional interface reference is declared.Syntaxinterface SomeFunc {  T func(T t); }Exampleinterface MyGeneric {    T compute(T t); ... Read More

What are the rules for a local variable in lambda expression in Java?

raja

raja

Updated on 11-Jul-2020 11:57:48

4K+ Views

A lambda expression can capture variables like local and anonymous classes. In other words, they have the same access to local variables of the enclosing scope. We need to follow some rules in case of local variables in lambda expressions.A lambda expression can't define any new scope as an anonymous inner class does, so we ... Read More

How to use an ArrayList in lambda expression in Java?

raja

raja

Updated on 11-Jul-2020 11:40:27

2K+ Views

The lambda expression is an inline code that can implement a functional interface without creating an anonymous class. An ArrayList can be used to store a dynamically sized collection of elements.In the below program, We have removed the elements of an ArrayList whose age is less than or equal to 20 using the removeIf() method. ... Read More

How can we use this and super keywords in method reference in Java?

raja

raja

Updated on 11-Jul-2020 08:55:49

1K+ Views

The method reference is similar to a lambda expression that refers to a method without executing it and "::" operator can be used to separate a method name from the name of an object or class in a method reference.The methods can be referenced with the help of this and super ... Read More

Advertisements