Raja has Published 806 Answers

How to implement ToDoubleBiFunction using lambda expression in Java?

raja

raja

Updated on 14-Jul-2020 13:39:07

ToDoubleBiFunction is a functional interface defined in java.util.function package. This functional interface accepts two parameters as input and produces a double-valued result. ToDoubleBiFunction interface can be used as an assignment target for a lambda expression or method reference. This interface contains only one abstract method: applyAsDouble() and doesn't contain any default or static methods.Syntax@FunctionalInterface interface ToDoubleBiFunction { ... Read More

How can we sort a Map by both key and value using lambda in Java?

raja

raja

Updated on 14-Jul-2020 13:30:27

A Map interface implements the Collection interface that provides the functionality of the map data structure. A Map doesn't contain any duplicate keys and each key is associated with a single value. We can able to access and modify values using the keys associated with them.In the below two examples, we can able to sort a Map by both ... Read More

How to implement LongUnaryOperator using lambda in Java?

raja

raja

Updated on 14-Jul-2020 13:27:20

LongUnaryOperator is a functional interface from java.util.function package. This functional interface accepts a single long-valued operand and produces a long-valued result. LongUnaryOperator interface can be used as an assignment target for lambda expression and method reference. It contains one abstract method: applyAsLong(), one static method: identity() and two default methods: andThen() and compose().Syntax@FunctionalInterface public interface LongUnaryOperator  long ... Read More

How to access variables from enclosing class using lambda in Java?

raja

raja

Updated on 14-Jul-2020 13:00:28

The variables defined by the enclosing scope of a lambda expression can be accessible within the lambda expression. A lambda expression can access to both instance, static variables and methods defined by the enclosing class. It has also access to "this" variable (both implicitly and explicitly) that can be an instance of enclosing class. The ... Read More

How to create a thread using method reference in Java?

raja

raja

Updated on 14-Jul-2020 12:54:42

Method reference is one of a way in a lambda expression to refer a method without executing it. In the body part of a lambda expression, we can call another method if they are compatible with a functional interface. We can also capture "this" and "super" keywords in method references.In the below two examples, ... Read More

How to implement DoubleUnaryOperator using lambda and method reference in Java?

raja

raja

Updated on 14-Jul-2020 12:48:28

DoubleUnaryOperator is a functional interface defined in java.util.function package. This functional interface expects a parameter of type double as input but produces the output of the same type. DoubleUnaryOperator interface can be used as an assignment target for lambda expression and method reference. This interface contains one abstract method: applyAsDouble(), one static method: identity() and two default methods: ... Read More

How to implement LongSupplier using lambda and method reference in Java?

raja

raja

Updated on 14-Jul-2020 12:25:24

LongSupplier is a built-in functional interface from java.util.function package. This interface doesn’t expect any input but produces a long-valued output. Since LongSupplier is a functional interface, it can be used as an assignment target for lambda expression and method reference and contains only one abstract method: getAsLong().Syntax@FunctionalInterface public interface LongSupplier {  long getAsLong(); }Example of Lambda Expressionimport java.util.function.LongSupplier; ... Read More

How to implement LongPredicate using lambda and method reference in Java?

raja

raja

Updated on 14-Jul-2020 12:06:47

LongPredicate is a functional interface defined in java.util.function package. This interface can be used mainly for evaluating an input of type long and returns an output of type boolean. LongPredicate can be used as an assignment target for a lambda expression or method reference. It contains one abstract method: test() and three default methods: and(), negate() ... Read More

How to create a constructor reference for an array in Java?

raja

raja

Updated on 14-Jul-2020 12:05:06

A constructor reference is similar to method reference except that the name of a method is new. We can also create a constructor reference with an array type. For instance, if we need to create an integer array by using the constructor reference: int[]:: new, where the parameter is a length of an array.SyntaxArrayTypeName[]::newExample@FunctionalInterface ... Read More

How to implement a constructor reference with one or more arguments in Java?

raja

raja

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

A method reference can also be applicable to constructors in Java 8. A constructor reference can be created using the class name and a new keyword. The constructor reference can be assigned to any functional interface reference that defines a method compatible with the constructor.Syntax::newExample of Constructor Reference with One Argumentimport java.util.function.*; @FunctionalInterface interface MyFunctionalInterface ... Read More

Advertisements