top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Predefined Functional Interfaces In Java

By using predefined functional interfaces we can invoke lambda expression in regular coding.

Predefined functional interfaces are :


  1. Predicate

  2. Function

  3. Consumer

  4. Supplier

These interfaces are in java.util.function package.


Predicate


This interface contains only one Abstract method and the method name is test().

Prototype of this interface is :

interface Predicate<input type>

{

public abstract boolean test(arg)

}

Note : Predicate always takes one parameter as input and returns boolean value. Use it only if you have any conditional checks in your program.


Examples :


package functional Interfaces;

import java.util.function. Predicate;

public class Predicate_Demo1 {
 public static void main(String[] args) {

  // Example 1

  Predicate<Integer> p = i -> (i > 10);
  System.out.println(p.test(30)); // o/p will be true

  // Example 2
  // Check the length of the given string is greater than 5

  Predicate<String> pr = s -> (s.length() > 4);
  System.out.println(pr.test("Abc")); // o/p will be false

  // Example 3
  // print array elements whose size is greater than 4

  String names[] = { "David", "Scott", "Smith", "John", "Mary" };
  for (String name : names) {
   if (pr.test(name)) {
    System.out.println(name);
   }
  }
 }

}

Output :



Lambda Expression for Object as an argument :


package functional Interfaces;

import java.util.ArrayList;
import java.util.function. Predicate;

class Employee {
 String name;
 int salary;
 int experience;

 Employee(String name, int salary, int experience) {
  this.name = name;
  this.salary = salary;
  this.experience = experience;
 }
}

public class Predicate_Demo2 {
 public static void main(String[] args) {
  Employee emp = new Employee("John", 50000, 5);
  Predicate<Employee> pr = e -> (e.salary > 30000 && e.experience > 3);
  // Here lambda expression takes employee object as an argument and check if
  // salary is greater than 30k
  // and experience is greater than 3
  System.out.println(pr.test(emp)); // o/p will be true

  // When multiple employee objects are created
  ArrayList<Employee> a1 = new ArrayList<Employee>();
  a1.add(new Employee("John", 50000, 5));
  a1.add(new Employee("David", 20000, 5));
  a1.add(new Employee("Scott", 40000, 3));
  a1.add(new Employee("Ben", 40000, 5));
  for (Employee e : a1) {
   if (pr.test(e)) {
    System.out.println(e.name + " " + e.salary);
   }
  }
 }

}

Output :



Function


This interface contains only one abstract method and the method name is apply(). It takes any type as an argument and it returns single value of any type.

Prototype of this interface is :

interface Function <Input type, Return type>

{

public Return apply(arg)

}

Note : If we want to do some operation and get some value , then we need to go for Function interface.


Examples :

package functional Interfaces;

import java.util.function. Function;

public class Function_Demo1 {
 public static void main(String[] args) {

  // Example 1
  Function<Integer, Integer> f = n -> n * n;
  System.out.println(f.apply(3)); // o/p will be 9

  // Example2
  Function<String, Integer> fn = s -> s.length();
  System.out.println(fn.apply("Hello")); // o/p will be 5
 }

}

Output :



Lambda Expression for Object as an argument :


package functional Interfaces;

import java.util.ArrayList;
import java.util.function. Function;

class Employee1 {
 String name;
 int salary;

 Employee1(String name, int salary) {
  this.name = name;
  this.salary = salary;
 }
}

public class Function_Demo2 {
 public static void main(String[] args) {
  ArrayList<Employee1> empList = new ArrayList<Employee1>();
  empList.add(new Employee1("David", 50000));
  empList.add(new Employee1("John", 30000));
  empList.add(new Employee1("Ben", 20000));
  Function<Employee1, Integer> fn = e -> {
   int sal = e.salary;
   if (sal >= 10000 && sal <= 20000) // Here if the salary is in between 10k and 20k then 10% of salary will
            // be returned as bonus.
    return (sal * 10 / 100);
   else if (sal >= 20000 && sal <= 30000)
    return (sal * 20 / 100);
   else if (sal >= 30000 && sal <= 50000)
    return (sal * 30 / 100);
   else
    return (sal * 40 / 100);
  };

  for (Employee1 emp : empList) {
   int bonus = fn.apply(emp);
   System.out.println(emp.name + " " + emp.salary);
   System.out.println("Bonus is " + bonus);
  }

 }

}
Out

Output :



Consumer


This interface will take one input as a parameter but it does not return any value. It contains only one abstract method and the method name is accept().


Example :

package functional_Interfaces;

import java.util.function.Consumer;

public class Consumer_Demo1 {

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

}

Output :



Supplier


This interface does not take any input value but it returns some value. It has only one abstract method and it’s name is get().


Summary :


In this blog , we have explored the below fundamental predefined functional interfaces introduced in Java 8, their single abstract method along with any default/static methods:


Predicate<T> interface with a test method that accepts an input of type T and returns a boolean value.

Function<T,R> interface with an apply method that accepts an input of type T and returns an output of type R.

Consumer<T> interface with an accept method that accepts one input of type T but doesn’t return any value.

Supplier<T> interface with a get method that doesn’t accept any input but supplies an output of type T.


13 views0 comments

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page