raja

raja

379 Articles Published

Articles by raja

Page 8 of 38

How to implement IntUnaryOperator using lambda in Java?

raja
raja
Updated on 13-Jul-2020 1K+ 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 * ...

Read More

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

raja
raja
Updated on 13-Jul-2020 1K+ 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(() -> { // ...

Read More

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

raja
raja
Updated on 13-Jul-2020 451 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

How to implement the IntPredicate interface using lambda and method reference in Java?

raja
raja
Updated on 13-Jul-2020 523 Views

IntPredicate interface is a built-in functional interface defined in java.util.function package. This functional interface accepts one int-valued argument as input and produces a boolean value as an output. This interface is a specialization of the Predicate interface and used as an assignment target for a lambda expression or method reference. It provides only one abstract method, test ().Syntax@FunctionalInterface public interface IntPredicate { boolean test(int value); }Example for Lambda Expressionimport java.util.function.IntPredicate; public class IntPredicateLambdaTest { public static void main(String[] args) { IntPredicate intPredicate = (int input) -> { ...

Read More

How to implement ActionEvent using method reference in JavaFX?

raja
raja
Updated on 13-Jul-2020 2K+ Views

The javafx.event package provides a framework for Java FX events. The Event class serves as the base class for JavaFX events and associated with each event is an event source, an event target, and an event type. An ActionEvent widely used when a button is pressed.In the below program, we can implement an ActionEvent for a button by using method reference.Exampleimport javafx.application.*; import javafx.beans.property.*; import javafx.event.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.*; import javafx.scene.effect.*; public class MethodReferenceJavaFXTest extends Application {    private Label label;    public static void main(String[] args) {       launch(args);    }    @Override    public void start(Stage primaryStage) {   ...

Read More

How to implement ToDoubleFunction using lambda expression in Java?

raja
raja
Updated on 13-Jul-2020 854 Views

ToDoubleFunction is a functional interface defined in java.util.function package. This functional interface expects a parameter as input and produces a double-valued result. It is used as an assignment target for a lambda expression or method reference. ToDoubleFunction interface contains only one abstract method, applyAsDouble().Syntax@FunctionalInterface public interface ToDoubleFunction { double applyAsDouble(T value); }Example-1import java.util.function.ToDoubleFunction; public class ToDoubleFunctionTest1 { public static void main(String args[]) { ToDoubleFunction strLength = s -> s.length(); // lambda expression System.out.println("The length of a given String ...

Read More

How to implement the Runnable interface using lambda expression in Java?

raja
raja
Updated on 13-Jul-2020 18K+ Views

The Runnable interface is a functional interface defined in java.lang package. This interface contains a single abstract method, run() with no arguments. When an object of a class implementing this interface used to create a thread, then run() method has invoked in a thread that executes separately.Syntax@FunctionalInterface public interface Runnable { void run(); }In the below example, we can implement a Runnable interface by using an anonymous class and lambda expression.Examplepublic class RunnableLambdaTest { public static void main(String[] args) { Runnable r1 = new Runnable() { ...

Read More

How to implement JavaFX event handling using lambda in Java?

raja
raja
Updated on 13-Jul-2020 4K+ Views

JavaFX Button class provides the setOnAction() method that can be used to set an action for the button click event. An EventHandler is a functional interface and holds only one method is the handle() method.Syntax@FunctionalInterface public interface EventHandler extends EventListenerIn the below example, we can able to implement event handling of JavaFX by using a lambda expression.Exampleimport javafx.application.*; import javafx.beans.property.*; import javafx.event.*; import javafx.scene.*; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.*; public class LambdaWithJavaFxTest extends Application {    public static void main(String[] args) {       Application.launch(args);    }    @Override    public void start(Stage stage) throws Exception {     ...

Read More

How to implement DoubleFunction using lambda expression in Java?

raja
raja
Updated on 13-Jul-2020 496 Views

The DoubleFunction is a built-in functional interface defined in java.util.function package. This interface accepts one double-valued parameter as input and returns a value of type R. As this is a functional interface, it can be used as an assignment target for a lambda expression or method reference. DoubleFunction interface having only one abstract method, apply().Syntax@FunctionalInterface public interface DoubleFunction {  R apply(double value); }Exampleimport java.util.function.DoubleFunction; public class DoubleFunctionTest {    public static void main(String[] args) {       DoubleFunction getGrade = marks -> { // lambda expression          if(marks > 90 && marks 70 && marks 50 && marks

Read More

How to generate prime numbers using lambda expression in Java?

raja
raja
Updated on 13-Jul-2020 2K+ Views

A prime number is a number that is greater than 1 and divided by 1 or itself only. It other words, it can't be divided by other numbers than itself or 1. The generation of prime numbers is 2, 3, 5, 7, 11, 13, 17 and etc.In the below example, we can generate the prime numbers with the help of Stream API and lambda expressions.Exampleimport java.util.*; import java.util.stream.*; public class PrimeNumberLambdaTest {    public static void main(String[] args) {       List generate = PrimeNumberLambdaTest.generate(10);       System.out.println(generate);    }    public static List generate(int series) {       Set set = new TreeSet(); ...

Read More
Showing 71–80 of 379 articles
« Prev 1 6 7 8 9 10 38 Next »
Advertisements