Package - java.util.function


Well you know predicate, function and consumer can only accept one input argument but sometimes we need a two-argument type functional interface in that situation we can use these predefined functional interfaces.


  1. BiPredicate
  2. BiFunction
  3. BiConsumer
BiPredicate: It is the same as a predicate interface but the only difference is it takes two input arguments to perform a conditional check.

1
2
3
 interface BiPredicate<T,U> {
public boolean test(T t, U u);
}


BiFunction: It is the same as a function predefine functional interface but the only difference is it takes two input arguments to perform some operation.

1
2
3
 interface BiFunction<T,U,R> {
public R apply(T t, U u);
}


BiConsumer: BiConsumer is exactly the same as Consumer except that it will take 2 input arguments

1
2
3
 interface BiConsumer<T,U> {
public void accept(T t, U u);
}





Java 8 : Supplier

What is a Supplier?

Supplier is a predefined functional interface like Consumerpresent in java.util.function package. It has one abstract method called get();

Supplier can be used to supply some value based on some operation, Supplier won't take any input and it will always supply objects.

1
2
3
interface Supplier<R> {
public R get();
}


Supplier example 1:


 1
2
3
4
5
6
7
8
9
10
11
12
package com.blogspot.ekumeedkiasha;

import java.util.function.Supplier;

public class SupplierExample1{

public static void main(String[] args) {
Supplier<Double> sup= () -> Math.random();
System.out.println(sup.get());
}

}



Property Predicate Function Consumer Supplier
What To take some Input and perform some conditional checks To take some Input and perform required Operation and return the result  To consume some Input and perform required Operation. It won’t return anything. To supply some Value base on our Requirement
Abstract method name  public boolean test (T t); public R apply (T t); public void accept (T t); public R get();
Default Methods and(), or(), negate() andThen(), compose() andThen() N/A
Static Method isEqual() identify() N/A N/A

Java 8 : Consumer

What is a Consumer?

Consumer is a predefined functional interface like Predicatepresent in java.util.function package. It has one abstract method called accept();

 Consumer can be used to consume object and perform a certain operation it takes one argument but won't return anything.


1
2
3
interface Consumer<T,R> {
public void accept(T t);
}


Consumer example 1: Take a single argument and print the value.

 1
2
3
4
5
6
7
8
9
10
11
12
package com.blogspot.ekumeedkiasha;

import java.util.function.Consumer;

public class ConsumerExample1 {

public static void main(String[] args) {
Consumer<String> con = s -> System.out.println(s);
con.accept("ekumeedhelp");
}

}


Consumer Chaining example:
 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.blogspot.ekumeedkiasha;

import java.util.function.Consumer;

public class ConsumerExample1 {

public static void main(String[] args) {

Consumer<String> con = s -> System.out.println(s.toUpperCase());

Consumer<String> con1 = s -> System.out.println("(" + s + ")");

con.andThen(con1).accept("ekumeed");

}

}