Raja has Published 469 Articles

How to use Supplier interface in lambda expression in Java?

raja

raja

Updated on 13-Jul-2020 07:22:11

7K+ Views

A Supplier interface is a pre-defined interface that represents the supplier of results. It is instantiated using lambda expression or method reference or default constructor. The functional method of a Supplier interface is the get() method. This interface belongs to java.util.function package.Syntax@FunctionalInterface public interface SupplierIn the below program, we can use the Supplier interface in a lambda expression. ... Read More

How to use Predicate and BiPredicate in lambda expression in Java?

raja

raja

Updated on 13-Jul-2020 07:18:22

2K+ Views

A Predicate interface defined in java.util.function package. It represents a boolean-valued function with one argument. It is kind of a functional interface whose functional method is the test(). BiPredicate interface is similar to the Predicate interface with two arguments. It can be used as an assignment target for a lambda expression.Syntax for Predicate@FunctionalInterface public interface PredicateExampleimport java.util.*; import java.util.function.*; ... Read More

How to sort a list by name using a Comparator interface in Java?

raja

raja

Updated on 13-Jul-2020 07:00:37

836 Views

Comparator interface can be used to order the objects of user-defined classes. It is capable of comparing two objects of two different classes. We can sort a list of objects where we can't modify the object’s source code to implement Comparable interface. A lambda expression can't be executed on its own and used to implement methods that ... Read More

How to use Function and BiFunction interfaces in lambda expression in Java?

raja

raja

Updated on 13-Jul-2020 06:37:00

2K+ Views

The Function interface is a pre-defined functional interface that can be used as an assignment target for a lambda expression or method reference. It takes a single parameter and returns result by calling the apply() method. While the BiFunction interface is also a pre-defined functional interface that takes two parameters and returns a result. It is similar ... Read More

How to use Consumer and BiConsumer interfaces in lambda expression in Java?

raja

raja

Updated on 13-Jul-2020 06:30:59

4K+ Views

A Consumer interface is a predefined functional interface that can be used when creating lambda expressions or method references. This interface represents an operation that accepts a single input parameter and doesn't return anything. It contains only one method named accept(). The BiConsumer interface is similar to a Consumer interface and accepts two input parameters and doesn’t return ... Read More

How to implement an instance method reference using a class name in Java?

raja

raja

Updated on 13-Jul-2020 05:40:25

828 Views

Method reference is a simplified form of the lambda expression. It can specify a class name or instance name followed by the method name. The "::" symbol can separate a method name from the name of an object or class.An instance method reference refers to an instance method of any class. In the below example, we can implement an ... Read More

How can we write Callable as a lambda expression in Java?

raja

raja

Updated on 13-Jul-2020 05:32:03

7K+ Views

A Callable interface defined in java.util.concurrent package. An object of Callable returns a computed result done by a thread in contrast to a Runnable interface that can only run the thread. The Callable object returns Future object that provides methods to monitor the progress of a task executed by a thread. An object of the Future used ... Read More

What are the SAM Interfaces in Java?

raja

raja

Updated on 13-Jul-2020 05:26:06

5K+ Views

An interface having only one abstract method is known as a functional interface and also named as Single Abstract Method Interfaces (SAM Interfaces). One abstract method means that either a default method or an abstract method whose implementation is available by default is allowed. The instances of SAM interfaces are java.lang.Runnable, java.awt.event.ActionListener,  java.util.Comparator ... Read More

How to implement reference to an instance method of a particular object in Java?

raja

raja

Updated on 11-Jul-2020 12:53:39

647 Views

Method reference is a simplified form of a lambda expression that can execute one method. It can be described using "::" symbol. A reference to the instance method of a particular object refers to a non-static method that is bound to a receiver.SyntaxObjectReference::instanceMethodNameExample - 1import java.util.*; public class InstanceMethodReferenceTest1 {   ... Read More

What are the rules for formal parameters in a lambda expression in Java?

raja

raja

Updated on 11-Jul-2020 12:50:17

355 Views

A lambda expression is similar to a method that has an argument, body, and return type. It can also be called an anonymous function (method without a name). We need to follow some rules while using formal parameters in a lambda expression.If the abstract method of functional interface is a zero-argument method, then the ... Read More

Advertisements