Java 8 features built in functional interfaces

  • 2020-05-19 04:54:16
  • OfStack

New Java 8 features built-in functional interfaces

The functional interface provided by Java 8 was mentioned in an Lambda expression in a previous blog post. In this article, I will introduce you to Java's 84 most basic functional interfaces

For method references, strictly speaking, you need to define an interface. No matter how we do it, there are actually only four possible interfaces.

Java 8 provides the functional interface package java.util.function.*, under which there are many built-in functional interfaces for Java 8. But there are basically four basic ones:

Functional interface (Function)

With T as input and R as output, it also includes the default method for combining with other functions.


@FunctionalInterface
public interface Function<T, R> {

  R apply(T t);

}
 

The sample code


   
public static void main(String[] args) {
    
    //  It's used here Java8 the   Method reference, functional function interface! 
    Function<String,Boolean> function = "Hello Java" :: endsWith;
    System.out.println(function.apply("Java"));
}
 

Consumer interface (Consumer)

Take T as input and return nothing, representing an operation on a single parameter.


@FunctionalInterface
public interface Consumer<T> {

  void accept(T t);

}
 

The sample code



class TestDemo{
  // This method does not return a value, but it does have input parameters 
  public void fun(String str){
    System.out.println(str);
  }
}


public class TestFunctional {
  public static void main(String[] args) {
    TestDemo demo = new TestDemo();
    // Consumer interface, only input parameters, no output parameters 
    Consumer<String> consumer = demo :: fun;
    consumer.accept("");

  }
}


Provisioning interface (Supplier)

There are no input parameters, only T returns output


@FunctionalInterface
public interface Supplier<T> {

  T get();

}

The sample code




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

    // Supplier type interface, only output parameters, no input parameters! 
    Supplier<String> supplier = "java 8" :: toUpperCase;
    System.out.println(supplier.get());
  }
}

Assertion interface (Predicate)

Taking T as input and returning a Boolean as output, the interface contains several default methods to combine Predicate into other complex logic (and, or, not).


@FunctionalInterface
public interface Predicate<T> {

  boolean test(T t);

}
 

The sample code


  public class TestFunctional {
  public static void main(String[] args) {
    
    // Assert type interface. There are input parameters and output parameters are Boolean values 
    Predicate<String> predicate = "Android" :: equalsIgnoreCase;
    System.out.println(predicate.test("android"));
    
  }
}

So in Java 8 there are four functional interfaces, so it is rarely up to the user to define a new functional interface!

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: