Standard (predefined) functional interfaces of the Java language
Contents
- 1. An overview of the Java standard interfaces. List of interfaces
- 2. Functional interface UnaryOperator<T>. Example
- 3. Functional interface BinaryOperator<T>. Example
- 4. Functional interface Consumer<T>. Example
- 5. Functional interface Supplier<T>. Example
- 6. Functional interface Function<T, R>. Example
- 7. Functional interface Predicate<T>. Example
- Related topics
Search other websites:
1. An overview of the Java standard interfaces. List of interfaces
Starting with JDK 8, Java has defined a number of standard (predefined) functional interfaces. These interfaces are widely used in standard Java libraries, in particular, in the StreamAPI data stream processing tools. The standard functional interfaces can also be used when developing your own programs.
To use the standard Java interface, you need to include the java.util.function package
import java.util.function.*;
Below is a list of standard functional interfaces.
- UnaryOperator<T> – performs a unary operation on an object of generic type T and returns a result of type T. Defines the apply() method;
- BinaryOperator<T> – Performs a boolean operation on two objects of type T and returns a result of the same type. Defines the apply() method;
- Consumer<T> – defines the accept() method that performs an operation on an object of type T;
- Supplier<T> – defines a get() method that returns an object of type T;
- Function<T, R> – defines the apply() method, which performs an operation on an object of type T and returns a result of type R;
- Predicate<T> – defines the test() method. The method receives an object of type T as a parameter and returns a boolean value. The test() method determines whether an object of type T satisfies some boolean condition.
⇑
2. Functional interface UnaryOperator<T>. Example
In the example, using the functional interface UnaryOperator<T>, a lambda expression is defined that returns the result of multiplying the input parameter by 2.
// Java predefined standard functional interfaces // Include package java.util.function import java.util.function.*; public class FunctionalInterfaces { public static void main(String[] args) { // Functional interface UnaryOperator<T> // Task. Implement a lambda expression that doubles the specified number. // 1. Declare a reference to UnaryOperator<T> and assign it a lambda expression UnaryOperator<Double> number = (n) -> n*2; // 2. Invoke method number.apply() Double t = number.apply(2.8); System.out.println("t = " + t); // t = 5.6 } }
⇑
3. Functional interface BinaryOperator<T>. Example
The BinaryOperator<T> functional interface defines the apply() method that takes two parameters of type T and returns a result of type T. The method performs some operation on parameters. The operation itself is implemented in a lambda expression.
The example demonstrates the BinaryOperator<T> interface that raises the first parameter to the power specified by the second parameter.
// Java predefined functional interfaces // Include package java.util.function import java.util.function.*; public class FunctionalInterfaces { public static void main(String[] args) { // Functional interface BinaryOperator<T> // Task. Implement a lambda expression that raises m to the power n. // 1. Declare a reference to BinaryOperator<T> BinaryOperator<Double> ref; // 2. Assign lambda expression to reference ref = (m, n) -> { return Math.pow(m, n); // invoke pow() function }; // 3. Invoke method number.apply() for parameters 3.0, 4.0 Double t = ref.apply(3.0, 4.0); System.out.println("t = " + t); // t = 81.0 } }
⇑
4. Functional interface Consumer<T>. Example
The functional interface Consumer<T> is designed to perform some action on the object of type T. The interface contains accept() method that receives the object of type T and returns a result of type void().
import java.util.function.*; public class TrainConsumer { public static void main(String[] args) { // Functional interface Consumer<T> // Task. Implement an action that reverses the integer i // and displays the result on the screen. // 1. Declare a reference to Consumer<T> for Integer type Consumer<Integer> cn; // 2. Implement a lambda-expression cn = (n) -> { Integer t = n, number = 0; while (t>0) { number = number * 10 + t % 10; t = t/10; } System.out.println("number = " + number); }; // 3. Invoke method accept() cn.accept(219); } }
The result of the program
number = 912
⇑
5. Functional interface Supplier<T>. Example
The Supplier<T> functional interface is designed to get an object of type T. The interface defines a get() method that returns an object of type T.
// Predefined functional interfaces Java // Include package java.util.function import java.util.function.*; public class FunctionalInterfaces { public static void main(String[] args) { // Standard interface Supplier<T> // 1. Get the object of type Integer // 1.1. Declare a reference of type Supplier<Integer> Supplier<Integer> ref; ref = () -> { return new Integer(23); }; // 1.2. Invoke method get() Integer objInt = ref.get(); System.out.println("objInt = " + objInt); // 2. Get the object of type Double[] - array of numbers // 2.1. Reference + lambda expression returning an array of 5 random numbers Supplier<Double[]> refArray = () -> { Double[] AD = new Double[5]; for (int i=0; i<AD.length; i++) AD[i] = Math.random()*100; return AD; // return the array of 5 numbers }; // 2.2. Invoke method get() Double[] AD2 = refArray.get(); // 2.3. Print array AD2 System.out.println("Array AD2."); for (Double x : AD2) System.out.print(x + " "); System.out.println(); } }
The result of the program
objInt = 23 Array AD2. 61.68462285137686 42.26246057548983 83.29368996444896 97.23604356051317 55.11066428763471
⇑
6. Functional interface Function<T, R>. Example
The functional interface Function<T, R> contains the apply() method. This method receives one parameter (object) of type T and returns a result (object) of type R. Below is an example of using this functional interface.
// Predefined functional interfaces Java // Include package java.util.function import java.util.function.*; public class FunctionalInterfaces { public static void main(String[] args) { // Functional interface Function<T, R> // Task 1. Implement the calculation of the root of a number // 1.1. Declare a reference to functional interface Function<T, R> Function<Integer, Double> ref1; // 1.2. Assign a lambda expression to this reference ref1 = n -> Math.sqrt(n); // 1.3. Demonstrate calculating the square root of 81 Double result1 = ref1.apply(81); System.out.println("result1 = " + result1); // result1 = 9.0 // ---------------------------------------------- // Task 2. Determine if a number is negative // 2.1. Declare a reference to the functional interface Function<T, R> Function<Double, Boolean> ref2; // 2.2. Assign a lambda expression to this reference ref2 = (n) -> { if (n<0) return true; return false; }; // 2.3. Demonstrate how a lambda expression works Boolean result2 = ref2.apply(-20.5); System.out.println("result2 = " + result2); } }
The result of the program
result1 = 9.0 result2 = true
⇑
7. Functional interface Predicate<T>. Example
Functional interface Pridicate<T> defines a test() method that takes a value of type T and returns boolean result. The test() method defines a test for a certain condition.
// Predefined functional interfaces Java // Include package java.util.function import java.util.function.*; public class FunctionalInterfaces { public static void main(String[] args) { // Functional interface Predicate<T> // 1. A reference to Predicate<Integer> Predicate<Integer> ref; // 2. A single lambda expression that determines if value is positive ref = (value) -> value>0; System.out.println("-37 > 0 = " + ref.test(-37)); // 3. A block lambda expression that determines whether value is in the range [0..99] ref = (value) -> { if ((value>=0)&&(value<=99)) return true; return false; }; boolean result = ref.test(77); System.out.println("value in the range [0..99] = " + result); } }
The result of the program
-37 > 0 = false value in the range [0..99] = true
⇑
Related topics
- Lambda expressions. Basic concepts. Functional interface. Examples
- Lambda expressions for generic functional interfaces. Examples
- Passing a lambda-expression to a method as a parameter. Examples
- Throwing exceptions in lambda expressions. Examples
⇑