Found 4747 Articles for Java 8

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

raja
Updated on 14-Jul-2020 13:58:35
IntToDoubleFunction is a functional interface from java.util.function package. This functional interface accepts an int-valued argument and produces a double-valued result. IntToDoubleFunction can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: applyAsDouble().Syntax@FunctionalInterface interface IntToDoubleFunction {    double applyAsDouble(int value); }Example of Lambda Expressionimport java.util.function.IntToDoubleFunction;; public class IntToDoubleLambdaTest {    public static void main(String[] args) {       IntToDoubleFunction getDouble = intVal -> {      // lambda expression          double doubleVal = intVal;          return doubleVal;       };       int input ... Read More

How to declare a variable within lambda expression in Java?

raja
Updated on 14-Jul-2020 13:53:51
A lambda expression is a function that expects and accepts input parameters and produces output results. It is an instance of a functional interface and also known as a single abstract method interface (SAM interface) like Runnable, Comparator, Callable and etc. We can declare a variable as a final string[] array and able to access that array index within a lambda expression.Exampleimport java.util.*; public class LambdaTest {    public static void main(String args[]) {       final String[] country = {null};       List cities = new ArrayList();       cities.add("Hyderabad");       cities.add("Ireland");       cities.add("Texas");       ... Read More

How to implement DoubleToLongFunction using lambda expression in Java?

raja
Updated on 14-Jul-2020 13:54:53
DoubleToLongFunction is a built-in functional interface from java.util.function package introduced in Java 8. This functional interface accepts a double-valued parameter and produces a long-valued result. DoubleToLongFunction interface can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: applyAsLong().Syntax@FunctionalInterface public interface DoubleToLongFunction {  long applyAsLong(double value) }Exampleimport java.util.function.DoubleToLongFunction; public class DoubleToLongFunctionTest {    public static void main(String args[]) {       double dbl = 30.1212;       DoubleToLongFunction castToLong = (dblValue) -> (long) dblValue; // lambda expression       System.out.println(castToLong.applyAsLong(dbl));       dbl = 77.9212;       DoubleToLongFunction roundToLong = Math::round;     ... Read More

How to implement DoubleToIntFunction using lambda expression in Java?

raja
Updated on 14-Jul-2020 13:55:31
DoubleToIntFunction is a functional interface defined in java.util.function package introduced in Java 8 version. This functional interface accepts a double-valued argument and produces an int-valued result. DoubleToIntFunction interface can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: applyAsInt().Syntax@FunctionalInterface interface DoubleToIntFunction {  int applyAsInt(double value) }Exampleimport java.util.function.DoubleToIntFunction; public class DoubleToIntFunctionTest {    public static void main(String args[]) {       DoubleToIntFunction test = doubleVal -> {     // lambda expression          int intVal = (int) doubleVal;          return intVal;       };       ... Read More

How to implement ToLongBiFunction using lambda expression in Java?

raja
Updated on 14-Jul-2020 13:50:14
ToLongBiFunction is a in-built functional interface from java.util.function package. This functional interface accepts two reference type parameters as input and produces a long-valued result. ToLongBiFunction interface can be used as an assignment target for a lambda expression or method reference and contains only one abstract method: applyAsLong().Syntax@FunctionalInterface interface ToLongBiFunction {  long applyAsLong(T t, U u); }Exampleimport java.util.*; import java.util.function.ToLongBiFunction; public class ToLongBiFunctionTest {    public static void main(String[] args) {       ToLongBiFunction getMobileNum = (map, value) -> {    // lambda          if(map != null && !map.isEmpty()) {             if(map.containsKey(value)) {       ... Read More

How to implement ToLongFunction using lambda expression in Java?

raja
Updated on 14-Jul-2020 13:45:09
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 More

How to implement ToDoubleBiFunction using lambda expression in Java?

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 {    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 More

How to implement ToIntBiFunction using lambda expression in Java?

raja
Updated on 14-Jul-2020 13:40:19
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 More

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

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 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 More

How to implement LongUnaryOperator using lambda in Java?

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 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 More
Advertisements