Found 4348 Articles for Java 8

What are the rules for a functional interface in Java?

raja
Updated on 14-Jul-2020 08:12:26

2K+ Views

A functional interface is a special kind of interface with exactly one abstract method in which lambda expression parameters and return types are matched. It provides target types for lambda expressions and method references.Rules for a functional interfaceA functional interface must have exactly one abstract method.A functional interface has any number of default methods because they are not abstract and implementation already provided by the same.A functional interface declares an abstract method overriding one of the public methods from java.lang.Object still considered as functional interface. The reason is any implementation class to this interface can have implementation for this abstract method either from ... Read More

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

raja
Updated on 14-Jul-2020 07:43:00

112 Views

LongBinaryOperator is a part of the functional interface from java.util.function package. This functional interface expects two parameters of type long as the input and produces a long type result. LongBinaryOperator 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 LongBinaryOperator {  long applyAsLong(long left, long right) }Example of Lambda Expressionimport java.util.function.LongBinaryOperator; public class LongBinaryOperatorTest1 {    public static void main(String[] args) {       LongBinaryOperator multiply = (a, b) -> { // lambda expression          return a*b;       };       long a = ... Read More

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

raja
Updated on 14-Jul-2020 07:31:43

237 Views

IntConsumer interface is a functional interface from java.util.function package in Java 8. This interface accepts a single int-valued argument as input but doesn't produce any output. Since it is a functional interface, it can be used as an assignment target for a lambda expression or method reference. It contains one abstract method: accept() and one default method: andThen().Syntax@FunctionalInterface public interface IntConsumer {    void accept(int value); }Example of Lambda Expressionimport java.util.function.IntConsumer; public class IntConsumerTest1 {    public static void main(String[] args) {       IntConsumer displayNextInt = i -> System.out.println("Next Int Value: " + (i+1));  // lambda       IntConsumer displaySquare = ... Read More

How to use this and super keywords with lambda expression in Java?

raja
Updated on 14-Jul-2020 07:32:30

1K+ Views

The "this" and "super" references within a lambda expression are the same as in the enclosing context. Since the lambda expression doesn't define a new scope, "this" keyword within a lambda expression signifies "this" parameter of a method where the lambda expression is residing.In the below example, this.toString() calls the toString() method of the LambdaTest object but not the toString() method of the Operate instance.Example@FunctionalInterface interface Operate {    int func(int num1, int num2);    public String toString(); } public class LambdaTest {    public static void main(String[] args) {       LambdaTest test = new LambdaTest();       test.getResult();    } ... Read More

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

raja
Updated on 16-Jan-2020 10:04:48

153 Views

DoublePredicate is a built-in functional interface defined in java.util.function package. This interface can accept one double-valued parameter as input and produces a boolean value as output. DoublePredicate interface can be used as an assignment target for a lambda expression or method reference. This interface contains one abstract method: test() and three default methods: and(),  or() and negate().Syntax@FunctionalInterface public interface DoublePredicate {    boolean test(double value) }Example of lambda expressionimport java.util.function.DoublePredicate; public class DoublePredicateLambdaTest {    public static void main(String args[]) {       DoublePredicate doublePredicate = (double input) -> {    // lambda expression          if(input == 2.0) {     ... Read More

How to implement DoubleConsumer using lambda expression in Java?

raja
Updated on 14-Jul-2020 06:38:16

174 Views

DoubleConsumer is a functional interface from java.util.function package. This functional interface accepts a single double-valued argument as input and produces no output. This interface can be used as an assignment target for a lambda expression or method reference. DoubleConsumer contains one abstract method: accept() and one default method: andThen().Syntax@FunctionalInterface public interface DoubleConsumer {    void accept(double value); }Example-1import java.util.function.DoubleConsumer; public class DoubleConsumerLambdaTest1 {    public static void main(String args[]) {       DoubleConsumer increment = doubleVal -> {       // lambda expression          System.out.println("Incrementing " + doubleVal + " by one");          System.out.println("Current Value : ... Read More

How to implement LongConsumer using lambda in Java?

raja
Updated on 13-Jul-2020 13:29:18

101 Views

LongConsumer is a in-built functional interface from java.util.function package. This interface can accept a single long-valued argument as input and doesn't produce any output. It can also be used as the assignment target for a lambda expression or method reference and contains one abstract method: accept() and one default method: andThen().Syntax@FunctionalInterface public interface LongConsumerExample-1import java.util.function.LongConsumer; public class LongConsumerLambdaTest {    public static void main(String[] args) {       LongConsumer displayNextVal = l-> {     // lambda expression          System.out.println("Display the next value to input : "+l);          System.out.println(l+1);       };       LongConsumer displayPrevVal ... Read More

How to implement IntUnaryOperator using lambda in Java?

raja
Updated on 13-Jul-2020 13:05:16

608 Views

IntUnaryOperator represents a functional interface that takes an int value and returns another int value. It is defined in java.util.function package and used as an assignment target for a lambda expression or method reference. It contains one abstract method: applyAsInt(), two default methods: compose(), andThen() and one static method: identity().Syntax@FunctionalInterface public interface IntUnaryOperatorExampleimport java.util.function.IntUnaryOperator; public class IntUnaryOperatorLambdaTest1 {    public static void main(String[] args) {       IntUnaryOperator getSquare = intValue -> {    // lambda expression          int result = intValue * intValue;          System.out.println("Getting square: "+ result);          return result;       };   ... Read More

How to write lambda expression code for SwingUtilities.invokeLater in Java?

raja
Updated on 13-Jul-2020 13:02:57

492 Views

An event handling code that runs on a special thread called event dispatch thread(EDT). Most of the code that invokes swing methods also runs on this EDT thread. It is necessary because most of swing object methods do not thread-safe. SwingUtilities is a utility class and has one important static method, invokeLater(). This method can be used to perform a task asynchronously in the AWT event dispatcher thread.Syntaxpublic static void invokeLater(Runnable doRun)Exampleimport javax.swing.*; public class InvokeLAterLambdaTest {    public static void main(String[] args) {       SwingUtilities.invokeLater(() -> {    // lambda expression code          JFrame frame = ... Read More

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

raja
Updated on 13-Jul-2020 12:28:40

223 Views

DoubleBinaryOperator is a functional interface defined in java.util.function package. It accepts two parameters of type double as input and produces another double value as a result. DoubleBinaryOperator interface can be used as an assignment target for a lambda expression or method reference and has only one abstract method applyAsDouble().Syntax@FunctionalInterface public interface DoubleBinaryOperator {  double applyAsDouble(double left, double right); }Example-1import java.util.function.DoubleBinaryOperator; public class DoubleBinaryOperatorTest {    public static void main(String args[]) {       // using lambda expression       DoubleBinaryOperator sum = (d1, d2) -> d1 + d2;       DoubleBinaryOperator mul = (d1, d2) -> d1 * d2;       DoubleBinaryOperator div = ... Read More

Previous 1 ... 4 5 6 7 8 ... 435 Next
Advertisements