Skip to main content

Posts

Showing posts from June, 2020

BiPredicate Functional Interface Java Examples

In this post we’ll see examples of java.util.function.BiPredicate functional interface. BiPredicate functional interface represents a boolean valued function that takes two arguments and returns either true or false. Abstract method in this functional interface is- test(T t, U u) - This method evaluates the predicate on the passed arguments and returns either true or false based on whether the input arguments match the predicate or not. If you are writing a Lambda expression that takes two arguments and uses those arguments to evaluate a condition that returns true or false then that lambda expression can be written as an implementation of BiPredicate built-in functional interface . Apart from the test(T t, U u) abstract method Predicate interface has following default interface methods . and(BiPredicate<? super T,? super U> other) - This default method returns a composed predicate that represents a short-circuiting logical AND of this predicate and another. or

Predicate Functional Interface Java Examples

In this post we’ll see examples of java.util.function.Predicate functional interface. Table of contents Methods in Predicate interface Predicate interface test() method example Predicate functional interface and() method example Predicate functional interface or() method example Predicate functional interface negate() method example Predicate functional interface in JDK Methods in Predicate interface Predicate functional interface represents a boolean valued function returning either true or false. Abstract method in this functional interface is- boolean test(T t) - This method evaluates the predicate on the passed argument and returns either true or false based on whether the input argument matches the predicate or not. If you are writing a Lambda expression that takes single argument and uses that argument to evaluate a condition that returns true or false then that lambda expression can be written as an implementation of Predicate built-in functi

BiFunction Functional Interface Java Examples

In this post we’ll see examples of java.util.function.BiFunction functional interface. BiFunction functional interface represents an operation that accepts two arguments and produces a result. Abstract method in this functional interface is- R apply(T t, U u) - Where t is the first argument, u is the second function argument and R denotes the type of the result that is returned. If you are writing a Lambda expression that takes two arguments of same or different types and returns a value of another type then that lambda expression can be written as an implementation of BiFunction built-in functional interface . Apart from the apply() abstract method Function() interface has following default interface method . andThen(Function<? super R,? extends V> after) - It takes another Function as argument and returns a composed BiFunction that performs, in sequence, first the operation of the calling BiFunction followed by the after operation. BiFunction interface apply() met

Function Functional Interface Java Examples

In this post we’ll see examples of java.util.function.Function functional interface. Function functional interface represents an operation that accepts one argument and produces a result. Abstract method in this functional interface is- R apply(T t) - Here T is the type of the argument passed to the method and it returns a value of type R. If you are writing a Lambda expression that takes single argument of one type and returns a value of another type (or even same type) then that lambda expression can be written as an implementation of Function built-in functional interface . Apart from the apply() abstract method Function() interface has following default and static methods. andThen(Function<? super R, ? extends V> after) - It is a default method which takes another Function as argument and returns a composed Function that performs, in sequence, first the operation of the calling Function followed by the after operation. compose(Function<? super V, ? extend

Supplier Functional Interface Java Examples

In this post we’ll see examples of java.util.function.Supplier functional interface. Supplier functional interface represents an operation that accepts no argument and supplies a result. In Supplier interface there is one abstract method get() and there is no default or static interface method . If you are writing a Lambda expression that needs a value returned then that lambda expression can be written as an implementation of Supplier built-in functional interface . Supplier functional interface examples 1. If you want some random number to be generated, that can be implemented as get() method of Supplier interface. import java.util.function.Supplier; public class SupplierExample { public static void main(String[] args) { Supplier<Double> randomNoGen = () -> Math.random(); System.out.println(randomNoGen.get()); System.out.println(randomNoGen.get()); } } Output 0.9507895772946557 0.11609076109430083 In the program statement; Supplier randomNoGe

BiConsumer Functional Interface Java Examples

In this post we’ll see examples of Java BiConsumer functional interface. BiConsumer functional interface represents an operation that accepts two arguments and returns no result. Abstract method in this functional interface is accept(T t, U u) and there is also one default method andThen(BiConsumer<? super T,? super U> after) . If you are writing a Lambda expression that needs two arguments and doesn’t return a value then that lambda expression can be written as an implementation of BiConsumer built-in functional interface . BiConsumer functional interface example 1. In the example a Map is created and then entries of the Map are displayed. We’ll use lambda expression that implements the BiConsumer functional interface. Lambda expression you write implements the abstract method of the functional interface so in the case of BiConsumer functional interface, lambda expression is implementing the accept() method. import java.util.HashMap; import java.util.Map; import ja

Java Consumer Functional Interface Examples

In this post we’ll see examples of Java Consumer functional interface which is one of the built-in functional interface . Consumer functional interface represents an operation that accepts a single argument and returns no result. Abstract method in this functional interface is accept(T t) and there is also one default method andThen() . Table of contents Consumer functional interface example Consumer functional interface andThen() method example Consumer functional interface in JDK Consumer functional interface example In the example a List of integer is created and then elements of the List are displayed. we’ll use lambda expression that implements the Consumer functional interface. Lambda expression you write implements the abstract method of the functional interface so in the case of Consumer functional interface, lambda expression is implementing the accept() method. import java.util.Arrays; import java.util.List; import java.util.function.Consumer; pu

Write a File Asynchronously in Java

This post shows how to Write a file asynchronously in Java using the java.nio.channels.AsynchronousFileChannel class. Using AsynchronousFileChannel you can create an asynchronous file channel for reading, writing, and manipulating a file. Writing file using AsynchronousFileChannel Just like reading a file asynchronously , for asynchronous writing also there are two write methods- One of the write() method returns a Future instance representing the result of an asynchronous computation. In another write() method CompletionHandler instance is passed as an argument which consumes the result of an asynchronous I/O operation. 1. Java program to write file asynchronously First we’ll use write method that returns Future instance. Future<Integer> write(ByteBuffer src, long position) - Writes a sequence of bytes to this channel from the given buffer (represented by src), starting at the given file position. The method returns a Future representing the pending res