Found 4348 Articles for Java 8

How to implement DoubleToIntFunction using lambda expression in Java?

raja
Updated on 14-Jul-2020 13:55:31

180 Views

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

133 Views

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

163 Views

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

179 Views

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

169 Views

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

704 Views

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

126 Views

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

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

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

596 Views

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 More

How to create a thread using method reference in Java?

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

525 Views

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, we can create a thread with the help of "this" and "super" keywords using method reference.Example of this keywordpublic class MethodRefThisTest {    public void runBody() {       for(int i = 1; i < 10; i++) {          System.out.println("Square of " + i + " ... Read More

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

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

164 Views

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 More

Advertisements