Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Java Articles
Page 168 of 450
How to implement ToLongFunction<T> using lambda expression in Java?
ToLongFunction is a functional interface defined in java.util.function package. This functional interface accepts a reference type as input and produces a long-valued result. ToLongFunction interface can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: applyAsLong().Syntax@FunctionalInterface interface ToLongFunction { long applyAsLong(T value); }Exampleimport java.util.*; import java.util.function.ToLongFunction; public class ToLongFunctionTest { public static void main(String args[]) { List list = new ArrayList(); list.add("11"); list.add("22"); list.add("33"); list.add("44"); list.add("55"); ToLongFunction function = (String item) -> Long.valueOf(item); // ...
Read MoreHow to implement ToIntBiFunction<T, U> using lambda expression in Java?
ToIntBiFunction is a built-in functional interface from java.util.function package. This interface accepts two parameters as input and produces an int-valued result. ToIntBiFunction interface can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: applyAsInt() and doesn't contain any default or static methods.Syntax@FunctionalInterface interface ToIntBiFunction { int applyAsInt(T t, U u); }Exampleimport java.util.function.ToIntBiFunction; public class ToIntBiFunctionTest { public static void main(String args[]) { ToIntBiFunction test = (t, u) -> t * u; System.out.println("The multiplication of t and u is: " + test.applyAsInt(10, 7)); System.out.println("The multiplication of t and u is: " ...
Read MoreHow to implement ToDoubleBiFunction<T, U> using lambda expression in Java?
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 { double applyAsDouble(T t, U u); }Exampleimport java.util.function.ToDoubleBiFunction; public class ToDoubleBiFunctionTest { public static void main(String args[]) { ToDoubleBiFunction test = (t, u) -> t / u; // lambda expression System.out.println("The division of t and u is: " + test.applyAsDouble(50, ...
Read MoreHow can we sort a Map by both key and value using lambda in Java?
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 key and value with the help of lambda expression.Example for sorting of Map using Keyimport java.util.*; import java.util.stream.*; public class MapSortUsingKeyTest { public static void main(String args[]) { // Sort a Map by their key Map map = new HashMap(); ...
Read MoreHow to implement LongUnaryOperator using lambda in Java?
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 applyAsLong(long operand); }Exampleimport java.util.function.LongUnaryOperator; public class LongUnaryOperatorTest { public static void main(String args[]) { LongUnaryOperator getSquare = longValue -> { // lambda long result = longValue * longValue; System.out.println("Getting square: " + result); ...
Read MoreHow to access variables from enclosing class using lambda in Java?
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 lambda expression also sets the value of an instance or static variable.Exampleinterface SimpleInterface { int func(); } public class SimpleLambdaTest { static int x = 50; public static void main(String[] args) { SimpleInterface test = () -> x; // Accessing of static variable ...
Read MoreHow to implement DoubleUnaryOperator using lambda and method reference in Java?
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: andThen() and compose().Syntax@FunctionalInterface public interface DoubleUnaryOperator { double applyAsDouble(double operand); }Example of Lambda Expressionimport java.util.function.DoubleUnaryOperator; public class DoubleUnaryOperatorTest1 { public static void main(String[] args) { DoubleUnaryOperator getDoubleValue = doubleValue -> { // lambda expression return doubleValue * 2; }; ...
Read MoreHow to implement LongSupplier using lambda and method reference in Java?
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; public class LongSupplierLambdaTest { public static void main(String args[]) { LongSupplier supplier = () -> { // lambda expression return 75; }; long result = supplier.getAsLong(); System.out.println(result); } }Output75Example ...
Read MoreHow to implement LongPredicate using lambda and method reference in Java?
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() and or().Syntax@FunctionalInterface interface LongPredicate { boolean test(long value); }Example of Lambda Expressionimport java.util.function.LongPredicate; public class LongPredicateLambdaTest { public static void main(String args[]) { LongPredicate longPredicate = (long input) -> { // lambda expression if(input == 50) { ...
Read MoreHow to implement IntBinaryOperator using lambda expression in Java?
IntBinaryOperator is a functional interface in Java 8 from java.util.function package. This interface expects two parameters of type int as input and produces an int type result. IntBinaryOperator can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: applyAsInt().Syntax@FunctionalInterface public interface IntBinaryOperator { int applyAsInt(int left, int right) }Exampleimport java.util.function.*; public class IntBinaryOperatorTest { public static void main(String[] args) { IntBinaryOperator test1 = (a, b) -> a + b; // lambda expression System.out.println("Addition of two parameters: " + test1.applyAsInt(10, 20)); IntFunction test2 = new IntFunction() { ...
Read More