Predefined Functional Interfaces

copy from: https://javatechonline.com/predefined-functional-interfaces

 1.  Predicate<T>

We can use Predicate<T> to implement some conditional checks. However, from it’s method signature : boolean test(T t) it is clear that it takes an input parameter and returns a Boolean result. When you have this type of requirement to write a method, use it confidently. Let’s observe the method signature as below:

 

public interface Predicate<T> {
    boolean test(T t);
}

For example, suppose we have to write a program to check if a number is a single digit number or not using Lambda expression. Since it’s a conditional check, we will implement Predicate as below:

Predicate<Integer> p = (i) -> (i > -10) && (i < 10);
System.out.println(p.test(9));

2. Function<T, R>

Function<T, R> is used to perform some operation & returns some result. Unlike Predicate<T> which returns only boolean, Function<T, R> can return any type of value. Therefore, we can also say that Predicate is a special type of Function which returns only Boolean values.

interface Function<T,R> { 
    R apply(T t);
}

For example, suppose we have to write a program to find length of a given String using Lambda expression. Since it’s taking input, performing some operation & returning result, we will implement Function as below:

Function<String, Integer> f = s -> s.length(); 
System.out.println(f.apply("I am happy now")); 

3. Consumer<T>

Consumer<T> is used when we have to provide some input parameter, perform certain operation, but don’t need to return anything. Moreover, we can use Consumer to consume object and perform certain operation.

interface Consumer<T> {
   void accept(T t);
}

For example, suppose we have to write a program to find length of a given String using Lambda expression. Since it’s taking input, performing some operation & returning result, we will implement Consumer as below:

Consumer<String> c = s -> System.out.println(s); 
c.accept("I consume data but don't return anything"); 

4. Supplier<R>

Supplier<R> doesn’t take any input and it always returns some object. However, we use it when we need to get some value based on some operation like supply Random numbers, supply Random OTPs, supply Random Passwords etc. For example, below code denotes it :

interface Supplier<R>{
    R get();
}

For example, suppose we have to write a program to supply 4 digit random OTPs using Lambda expression. Since it will return values without taking any input parameter, we will implement Supplier as below:

Supplier<String> otps = () -> {
     String otp = "";
     for (int i = 1; i <= 4; i++) {
        otp = otp + (int) (Math.random() * 10);
     }
   return otp;
};
System.out.println(otps.get());
System.out.println(otps.get());
System.out.println(otps.get());

5. BiPredicate<T, U>

BiPredicate<T, U> is same as Predicate<T> except that it has two input parameters. For example, below code denotes it:

interface BiPredicate<T, U> {
    boolean test(T t, U u)
}
For example, suppose we have to check the sum of two integers is even or not by using BiPredicate, we will implement BiPredicate<T, U> as below:
BiPredicate<Integer,Integer> bp = (i,j)->(i+j) %2==0; 
System.out.println(bp.test(24,34)); 

6. BiFunction<T, U, R>

BiFunction<T, U, R> is same as Function<T, R> except that it has two input parameters. For example, below code denotes it:

interface BiFunction<T, U, R> {
    R apply(T t, U u);
}
For example, suppose we have to find sum of two integers by using BiFunction, we will implement BiFunction<T,U,R> as below:
BiFunction<Integer,Integer,Integer> bf = (i,j)->i+j; 
System.out.println(bf.apply(24,4)); 

7. BiConsumer<T, U>

BiConsumer<T> is same as Consumer<T> except that it has two input parameters. For example, below code denotes it:

interface BiConsumer<T, U> {
   void accept(T t, U u);
}
For example, suppose we have to find concatenation of two strings & print result on the console by using BiConsumer, we will implement BiConsumer<T, U> as below:
BiConsumer<String,String> bc = (s1, s2)->System.out.println(s1+s2); 
bc.accept("Bi","Consumer"); 

Other default & static methods of Predefined Functional Interfaces

However, It is just to remind from the concept of Functional Interfaces that in addition to a single abstract method they can also have static & default methods as well without violating the rules of Functional Interfaces. Consequently, here we will list all of them for each Functional Interface.

Predicate<T>

default Predicate<T> and(Predicate<? super T> other)

♦Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.

static <T> Predicate<T> isEqual(Object targetRef)

⇒Returns a predicate that tests if two arguments are equal according to Objects.equals(Object, Object).

default Predicate<T> negate()

♦Returns a predicate that represents the logical negation of this predicate.

default Predicate<T> or(Predicate<? super T> other)

⇒Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.

Function<T,R>

default <V> Function<T, V> andThen(Function<? super R, ? extends V> after)

⇒Returns a composed function that first applies this function to its input, and then applies the after function to the result.

default <V> Function<V, R> compose(Function<? super V, ? extends T> before)

♦Returns a composed function that first applies the before function to its input, and then applies this function to the result.

static <T> Function<T, T> identlity()

⇒Returns a function that always returns its input argument.

Consumer<T>

default Consumer<T> andThen(Consumer<? super T> after)

⇒Returns a composed Consumer that performs, then in sequence, this operation followed by the after operation.

Supplier<R>

There are no default & static methods in this interface.

BiPredicate<T, U>

default BiPredicate<T, U> and(BiPredicate<? super T, ? super U> other)

⇒Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.

default BiPredicate<T, U> negate()

♦Returns a predicate that represents the logical negation of this predicate.

default BiPredicate<T, U> or(BiPredicate<? super T, ? super U> other)

⇒Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.

BiFunction<T, U, R>

default <V> BiFunction<T, U, V> andThen(Function<? super R, ? extends V> after)

♦Returns a composed function that first applies this function to its input, and then applies the after function to the result.

 

BiConsumer<T, U>

default BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after)

⇒Returns a composed BiConsumer that performs, in sequence, this operation followed by the after operation.

 

posted @   saaspeter  阅读(15)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示