Skip to main content
Functional interfaces have a single functionality to exhibit. For example, a Comparable interface with a single method ‘compareTo’ is used for comparison purpose. Java 8 has defined a lot of functional interfaces to be used extensively in lambda expressions. Following is the list of functional interfaces defined in java.util.Function package.
S. No.Interface & Description
1BiConsumer<T,U>
Represents an operation that accepts two input arguments, and returns no result.
2BiFunction<T,U,R>
Represents a function that accepts two arguments and produces a result.
3BinaryOperator<T>
Represents an operation upon two operands of the same type, producing a result of the same type as the operands.
4BiPredicate<T,U>
Represents a predicate (Boolean-valued function) of two arguments.
5BooleanSupplier
Represents a supplier of Boolean-valued results.
6Consumer<T>
Represents an operation that accepts a single input argument and returns no result.
7DoubleBinaryOperator
Represents an operation upon two double-valued operands and producing a double-valued result.
8DoubleConsumer
Represents an operation that accepts a single double-valued argument and returns no result.
9DoubleFunction<R>
Represents a function that accepts a double-valued argument and produces a result.
10DoublePredicate
Represents a predicate (Boolean-valued function) of one double-valued argument.
11DoubleSupplier
Represents a supplier of double-valued results.
12DoubleToIntFunction
Represents a function that accepts a double-valued argument and produces an int-valued result.
13DoubleToLongFunction
Represents a function that accepts a double-valued argument and produces a long-valued result.
14DoubleUnaryOperator
Represents an operation on a single double-valued operand that produces a double-valued result.
15Function<T,R>
Represents a function that accepts one argument and produces a result.
16IntBinaryOperator
Represents an operation upon two int-valued operands and produces an int-valued result.
17IntConsumer
Represents an operation that accepts a single int-valued argument and returns no result.
18IntFunction<R>
Represents a function that accepts an int-valued argument and produces a result.
19IntPredicate
Represents a predicate (Boolean-valued function) of one int-valued argument.
20IntSupplier
Represents a supplier of int-valued results.
21IntToDoubleFunction
Represents a function that accepts an int-valued argument and produces a double-valued result.
22IntToLongFunction
Represents a function that accepts an int-valued argument and produces a long-valued result.
23IntUnaryOperator
Represents an operation on a single int-valued operand that produces an int-valued result.
24LongBinaryOperator
Represents an operation upon two long-valued operands and produces a long-valued result.
25LongConsumer
Represents an operation that accepts a single long-valued argument and returns no result.
26LongFunction<R>
Represents a function that accepts a long-valued argument and produces a result.
27LongPredicate
Represents a predicate (Boolean-valued function) of one long-valued argument.
28LongSupplier
Represents a supplier of long-valued results.
29LongToDoubleFunction
Represents a function that accepts a long-valued argument and produces a double-valued result.
30LongToIntFunction
Represents a function that accepts a long-valued argument and produces an int-valued result.
31LongUnaryOperator
Represents an operation on a single long-valued operand that produces a long-valued result.
32ObjDoubleConsumer<T>
Represents an operation that accepts an object-valued and a double-valued argument, and returns no result.
33ObjIntConsumer<T>
Represents an operation that accepts an object-valued and an int-valued argument, and returns no result.
34ObjLongConsumer<T>
Represents an operation that accepts an object-valued and a long-valued argument, and returns no result.
35Predicate<T>
Represents a predicate (Boolean-valued function) of one argument.
36Supplier<T>
Represents a supplier of results.
37ToDoubleBiFunction<T,U>
Represents a function that accepts two arguments and produces a double-valued result.
38ToDoubleFunction<T>
Represents a function that produces a double-valued result.
39ToIntBiFunction<T,U>
Represents a function that accepts two arguments and produces an int-valued result.
40ToIntFunction<T>
Represents a function that produces an int-valued result.
41ToLongBiFunction<T,U>
Represents a function that accepts two arguments and produces a long-valued result.
42ToLongFunction<T>
Represents a function that produces a long-valued result.
43UnaryOperator<T>
Represents an operation on a single operand that produces a result of the same type as its operand.

Functional Interface Example

Predicate <T> interface is a functional interface with a method test(Object) to return a Boolean value. This interface signifies that an object is tested to be true or false.
To get more clarity on this, write the following program in an code editor and verify the results.

Java8Tester.java

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class Java8Tester {
   public static void main(String args[]){
      List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
  
      // Predicate<Integer> predicate = n -> true
      // n is passed as parameter to test method of Predicate interface
      // test method will always return true no matter what value n has.
  
      System.out.println("Print all numbers:");
  
      //pass n as parameter
      eval(list, n->true);
  
      // Predicate<Integer> predicate1 = n -> n%2 == 0
      // n is passed as parameter to test method of Predicate interface
      // test method will return true if n%2 comes to be zero
  
      System.out.println("Print even numbers:");
      eval(list, n-> n%2 == 0 );
  
      // Predicate<Integer> predicate2 = n -> n > 3
      // n is passed as parameter to test method of Predicate interface
      // test method will return true if n is greater than 3.
  
      System.out.println("Print numbers greater than 3:");
      eval(list, n-> n > 3 );
   }
 
   public static void eval(List<Integer> list, Predicate<Integer> predicate) {
      for(Integer n: list) {
  
         if(predicate.test(n)) {
            System.out.println(n + " ");
         }
      }
   }
}
Here we've passed Predicate interface, which takes a single input and returns Boolean.

Verify the Result

Compile the class using javac compiler as follows −
$javac Java8Tester.java
Now run the Java8Tester as follows −
$java Java8Tester
It should produce the following output −
Print all numbers:
1
2
3
4
5
6
7
8
9
Print even numbers:
2
4
6
8
Print numbers greater than 3:
4
5
6
7
8
9

Comments

Popular posts from this blog

In this chapter, we will see how to enable remote desktop application. It is important because this enables us to work remotely on the server. To do this, we have the following two options. For the first option, we have to follow the steps given below. Step 1  − Go to Start → right click “This PC” → Properties. Step 2  − On Left side click “Remote Setting”. Step 3  − Check radio button “Allow Remote connection to this computer” and Check box “Allow connection only from computers running Remote Desktop with Network Level Authentication (recommended)” → click “Select Users”. Step 4  − Click Add. Step 5  − Type user that you want to allow access. In my case, it is administrator → click OK. For the  second option , we need to follow the steps given below. Step 1  − Click on “Server Manage” → Local Server → click on “Enable” or Disable, if it is Disabled.
The table creation command requires: Name of the table Names of fields Definitions for each field Syntax: Here is generic SQL syntax to create a MySQL table: CREATE TABLE table_name ( column_name column_type ); Now, we will create following table in  TUTORIALS  database. tutorials_tbl ( tutorial_id INT NOT NULL AUTO_INCREMENT , tutorial_title VARCHAR ( 100 ) NOT NULL , tutorial_author VARCHAR ( 40 ) NOT NULL , submission_date DATE , PRIMARY KEY ( tutorial_id ) ); Here few items need explanation: Field Attribute  NOT NULL  is being used because we do not want this field to be NULL. So if user will try to create a record with NULL value, then MySQL will raise an error. Field Attribute  AUTO_INCREMENT  tells MySQL to go ahead and add the next available number to the id field. Keyword  PRIMARY KEY  is used to define a column as primary key. You can use multiple columns separated by comma to define...
The Windows Firewall with Advanced Security is a firewall that runs on the Windows Server 2012 and is turned on by default. The Firewall settings within Windows Server 2012 are managed from within the  Windows Firewall Microsoft Management Console . To set Firewall settings perform the following steps − Step 1  − Click on the Server Manager from the task bar → Click the Tools menu and select Windows Firewall with Advanced Security. Step 2  − To see the current configuration settings by selecting  Windows Firewall Properties  from the MMC. This  allows access to modify the settings  for each of the three firewall profiles, which are –  Domain, Private and Public  and IPsec settings. Step 3  − Applying custom rules, which will include the following two steps − Select either  Inbound Rules  or  Outbound Rules  under  Windows Firewall with Advanced Security  on the left side of the management console...