Found 4348 Articles for Java 8

How to implement ActionEvent using method reference in JavaFX?

raja
Updated on 13-Jul-2020 12:16:32

1K+ 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 the IntPredicate interface using lambda and method reference in Java?

raja
Updated on 13-Jul-2020 12:17:37

227 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) -> {   // lambda expression          if(input == 100) {     ... Read More

How to implement ToDoubleFunction using lambda expression in Java?

raja
Updated on 13-Jul-2020 12:10:26

544 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 using lambda expression is: " + strLength.applyAsDouble("TutorialsPoint"));       ToDoubleFunction innerClassImplementation = new ToDoubleFunction() ... Read More

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

raja
Updated on 13-Jul-2020 12:03:19

12K+ 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() {          @Override          public void run() { // anonymous class   ... Read More

How to implement JavaFX event handling using lambda in Java?

raja
Updated on 13-Jul-2020 11:54:58

3K+ 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
Updated on 13-Jul-2020 11:44:48

237 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

How to generate prime numbers using lambda expression in Java?

raja
Updated on 13-Jul-2020 11:32:07

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

How to implement the Fibonacci series using lambda expression in Java?

raja
Updated on 13-Jul-2020 09:24:29

2K+ Views

The Fibonacci is a sequence of numbers in which every number after the first two numbers is the sum of the two preceding numbers like 0, 1, 1, 2, 3, 5, 8, 13, 21 and so on. The sequence of Fibonacci numbers defined by using "F(n)=F(n-1)+F(n-2)".In the below example, we can implement the Fibonacci series with the help of Stream API and lambda expression. The Stream.iterate() method returns an infinite sequential ordered stream produced by iterative application of a function to an initial element seed, producing a stream consisting of seed, f(seed), f(f(seed)), etc.Exampleimport java.util.List; import java.util.stream.*; public class FibonacciTest {    public static void ... Read More

How to implement IntFunction with lambda expression in Java?

raja
Updated on 13-Jul-2020 09:25:13

336 Views

IntFunction interface is a functional interface in Java 8 and defined in java.util.function package. This interface accepts an int-valued parameter as input and returns a value of type R. IntFunction interface can be used as an assignment target for a lambda expression or method reference and holds only one abstract method, apply().Syntax@FunctionalInterface public interface IntFunction {    R apply(int value); }Exampleimport java.util.HashMap; import java.util.Map; import java.util.function.IntFunction; public class IntFunctionTest {    public static void main(String[] args) {       IntFunction getMonthName = monthNo -> {  // lambda expression          Map months = new HashMap();          months.put(1, "January");       ... Read More

How to sort a list using Comparator with method reference in Java 8?

raja
Updated on 13-Jul-2020 09:23:06

1K+ Views

Java 8 introduced changes in the Comparator interface that allows us to compare two objects. These changes help us to create comparators more easily. The first important method added is the comparing() method. This method receives as a parameter Function that determines the value to be compared and creates Comparator. Another important method is the thenComparing() method. This method can be used to compose Comparator.In the below example, we can sort a list by using the first name with comparing() method and then the last name with the thenComparing() method of Comparator interface.Exampleimport java.util.*; public class MethodReferenceSortTest {    public static void main(String[] ... Read More

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