What are the in-built functional interfaces in Java?


The java.util.function package defines several in-built functional interfaces that can be used when creating lambda expressions or method references.

Inbuilt functional interfaces:

1) Function Interface

The Function interface has only one single method apply(). It can accept an object of any data type and returns a result of any datatype.

Example

import java.util.*;
import java.util.function.*;

public class FunctionTest {
   public static void main(String args[]) {
      String[] countries = {"India", "Australia", "England", "South Africa", "Srilanka", "Newzealand", "West Indies", "Scotland"};
      Function<String[], String> converter = (all) -> {  // lambda expression
         String names = "";
         for(String n : all) {
            String result = n.substring(0, n.indexOf(""));
            result = n.substring(n.indexOf("")) + " " + result;
            names += result + "
";          }          return names;       };       System.out.println(converter.apply(countries));    } }

Output

India
Australia
England
South Africa
Srilanka
Newzealand
West Indies
Scotland


2) Supplier Interface

A Supplier interface has only one single method called get(). It does not accept any arguments and returns an object of any data type.

Example

import java.util.*;
import java.util.function.*;

public class SupplierTest {
   private static void printNames(Supplier<String> arg) {
      System.out.println(arg.get());
   }
   private static void listBeginWith(List<String> list, Predicate<String> valid) {
      printNames(() -> "
List of countries:");       list.forEach(country -> {     // lambda expression          if(valid.test(country)) {             printNames(() -> country);          }       });    }    public static void main(String[] args) {       String[] countries = {"India", "Australia", "England", "South Africa", "Srilanka", "Newzealand", "West Indies"};       List<String> countryList = Arrays.asList(countries);       listBeginWith(countryList, (s) -> s.startsWith("I"));       listBeginWith(countryList, (s) -> s.contains("I"));       listBeginWith(countryList, (s) -> s.endsWith("ia"));    } }

Output

List of countries:
India

List of countries:
India
West Indies

List of countries:
India
Australia


3) Consumer Interface

The Consumer interface has only one single method called accept(). It accepts a single argument of any data type and does not return any result.

Example

import java.util.*;
import java.util.function.*;

public class ConsumerTest {
   public static void main(String[] args) {
      String[] countries = {"India", "Australia", "England", "South Africa", "Srilanka", "Newzealand", "West Indies"};
      System.out.print("The list of countries:
");       Arrays.asList(countries).forEach((country) -> System.out.println(country)); // lambda expression    } }

Output

The list of countries:
India
Australia
England
South Africa
Srilanka
Newzealand
West Indies


4) Predicate Interface

The Predicate interface has only one single method test(). It may be true or false depending on the values of its variables.

Example

import java.util.*;
import java.util.function.*;

public class PredicateTest {
   private static List getBeginWith(List<String> list, Predicate<String> valid) {
      List<String> selected = new ArrayList<>();
      list.forEach(country -> {     // lambda expression
         if(valid.test(country)) {
            selected.add(country);
         }
      });
      return selected;
   }
   public static void main(String[] args) {
      String[] countries = {"India", "Australia", "England", "South Africa", "Srilanka", "Newzealand", "West Indies"};
      List<String> countryList = Arrays.asList(countries);
         System.out.println(getBeginWith(countryList, (s) -> s.startsWith("A")));
         System.out.println(getBeginWith(countryList, (s) -> s.contains("W")));
         System.out.println(getBeginWith(countryList, (s) -> s.endsWith("nd")));
   }
}

Output

[Australia]
[West Indies]
[England, Newzealand]

Updated on: 10-Jul-2020

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements