Java Tutorial
- Java - Home
- Java - Overview
- Java - History
- Java - Features
- Java vs C++
- Java Virtual Machine (JVM)
- Java - JDK vs JRE vs JVM
- Java - Hello World Program
- Java - Environment Setup
- Java - Basic Syntax
- Java - Variable Types
- Java - Data Types
- Java - Type Casting
- Java - Unicode System
- Java - Basic Operators
- Java - Comments
Java Control Statements
- Java - Loop Control
- Java - Decision Making
- Java - If-else
- Java - Switch
- Java - For Loops
- Java - For-Each Loops
- Java - While Loops
- Java - do-while Loops
- Java - Break
- Java - Continue
Object Oriented Programming
- Java - OOPs Concepts
- Java - Object & Classes
- Java - Class Attributes
- Java - Class Methods
- Java - Methods
- Java - Variables Scope
- Java - Constructors
- Java - Access Modifiers
- Java - Inheritance
- Java - Aggregation
- Java - Polymorphism
- Java - Overriding
- Java - Method Overloading
- Java - Dynamic Binding
- Java - Static Binding
- Java - Instance Initializer Block
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java - Inner Classes
- Java - Static Class
- Java - Anonymous Class
- Java - Singleton Class
- Java - Wrapper Classes
- Java - Enums
- Java - Enum Constructor
- Java - Enum Strings
Java Built-in Classes
Java File Handling
- Java - Files
- Java - Create a File
- Java - Write to File
- Java - Read Files
- Java - Delete Files
- Java - Directories
- Java - I/O Streams
Java Error & Exceptions
- Java - Exceptions
- Java - try-catch Block
- Java - try-with-resources
- Java - Multi-catch Block
- Java - Nested try Block
- Java - Finally Block
- Java - throw Exception
- Java - Exception Propagation
- Java - Built-in Exceptions
- Java - Custom Exception
Java Multithreading
- Java - Multithreading
- Java - Thread Life Cycle
- Java - Creating a Thread
- Java - Starting a Thread
- Java - Joining Threads
- Java - Naming Thread
- Java - Thread Scheduler
- Java - Thread Pools
- Java - Main Thread
- Java - Thread Priority
- Java - Daemon Threads
- Java - Thread Group
- Java - Shutdown Hook
Java Synchronization
- Java - Synchronization
- Java - Block Synchronization
- Java - Static Synchronization
- Java - Inter-thread Communication
- Java - Thread Deadlock
- Java - Interrupting a Thread
- Java - Thread Control
- Java - Reentrant Monitor
Java Networking
- Java - Networking
- Java - Socket Programming
- Java - URL Processing
- Java - URL Class
- Java - URLConnection Class
- Java - HttpURLConnection Class
- Java - Socket Class
- Java - Generics
Java Collections
Java Interfaces
- Java - List Interface
- Java - Queue Interface
- Java - Map Interface
- Java - SortedMap Interface
- Java - Set Interface
- Java - SortedSet Interface
Java Data Structures
Java Collections Algorithms
Advanced Java
- Java - Command-Line Arguments
- Java - Lambda Expressions
- Java - Sending Email
- Java - Applet Basics
- Java - Javadoc Comments
- Java - Autoboxing and Unboxing
- Java - File Mismatch Method
- Java - REPL (JShell)
- Java - Multi-Release Jar Files
- Java - Private Interface Methods
- Java - Inner Class Diamond Operator
- Java - Multiresolution Image API
- Java - Collection Factory Methods
- Java - Module System
- Java - Nashorn JavaScript
- Java - Optional Class
- Java - Method References
- Java - Functional Interfaces
- Java - Default Methods
- Java - Base64 Encode Decode
- Java - Switch Expressions
- Java - Teeing Collectors
- Java - Microbenchmark
- Java - Text Blocks
- Java - Dynamic CDS archive
- Java - Z Garbage Collector (ZGC)
- Java - Null Pointer Exception
- Java - Packaging Tools
- Java - Sealed Classes
- Java - Record Classes
- Java - Hidden Classes
- Java - Pattern Matching
- Java - Compact Number Formatting
- Java - Garbage Collection
- Java - JIT Compiler
Java Miscellaneous
- Java - Recursion
- Java - Regular Expressions
- Java - Serialization
- Java - Strings
- Java - Process API Improvements
- Java - Stream API Improvements
- Java - Enhanced @Deprecated Annotation
- Java - CompletableFuture API Improvements
- Java - Streams
- Java - Datetime Api
- Java 8 - New Features
- Java 9 - New Features
- Java 10 - New Features
- Java 11 - New Features
- Java 12 - New Features
- Java 13 - New Features
- Java 14 - New Features
- Java 15 - New Features
- Java 16 - New Features
Java APIs & Frameworks
Java Class References
- Java - Scanner Class
- Java - Arrays Class
- Java - Strings
- Java - Date & Time
- Java - ArrayList
- Java - Vector Class
- Java - Stack Class
- Java - PriorityQueue
- Java - LinkedList
- Java - ArrayDeque
- Java - HashMap
- Java - LinkedHashMap
- Java - WeakHashMap
- Java - EnumMap
- Java - TreeMap
- Java - The IdentityHashMap Class
- Java - HashSet
- Java - EnumSet
- Java - LinkedHashSet
- Java - TreeSet
- Java - BitSet Class
- Java - Dictionary
- Java - Hashtable
- Java - Properties
- Java - Collection Interface
- Java - Array Methods
Java Useful Resources
Java - Functional Interfaces
Functional interfaces were introduced in Java 8 along with lambda expression and method references. These three features were added to boost functional programming in Java and to write clean, readable code. Before Java 8, a lot of boilerplate code had to be written to cover basic functionality. For example, in order to call a function, first we had to create a class with the required method, create a class instance, and the instance, needed to invoke the method or another way was to use an anonymous class with the corresponding method.
With lambda expression, we can avoid the need for both concrete as well as anonymous class objects. A functional interface assists one step ahead, as a lambda expression can implement the functional interface easily as only one method is to be implemented.
Functional interfaces have a single functionality to exhibit. For example, a Comparable interface with a single method compareTo() is used for comparison purposes. But it can have any number of default and static methods.
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.
@FunctionalInterface Annotation
By functionality, any interface having a single abstract method is a functional interface. Java provides a @FunctionalInterface annotation to mark an interface as a functional interface so that the compiler can check if an interface is a functional interface or not. This annotation is optional and is primarily to add a compiler check and to increase the code readability and maintenance.
Types of Functional Interfaces in Java
There are primarily four types of functional interfaces in Java.
Predicate Functional Interface
A predicate functional interface is the one whose method accepts one argument and will return either true or false. A predicate functional interface is mainly used in comparison to sort elements or to filter a value based on certain condition(s) applied on the input passed. Java provides predicate functional interfaces for primitives as well as IntPredicate, DoublePredicate, and LongPredicate, which accept only Integer, Double, and Long respectively.
Usage
Predicate predicate = (value) -> value != 0; // or Predicate predicate = (value) -> test(value);
In above snippet predicate function is to return either true/false based on the value passed.
Example
In this example, we’re using a predicate functional interface to filter odd numbers from a list of integers with the help of a lambda expression.
package com.tutorialspoint; import java.util.Arrays; import java.util.List; import java.util.function.Predicate; public class Tester { public static void main(String args[]) { List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8); Predicate<Integer> isEvenNumber = n -> n %2 == 0; numbers = numbers.stream().filter(isEvenNumber).toList(); System.out.println(numbers); } }
Let us compile and run the above program, this will produce the following result −
[2, 4, 6, 8]
Consumer Functional Interface
A consumer functional interface is one whose method accepts one argument and will not return anything. A consumer functional interface is mainly used for side-effect operations. For example, to print an element, to add a salutation, etc. There are other variants of Consumer as well like BiConsumer. A BiConsumer functional interface can accept two parameters. Java provides consumer functional interfaces for primitives as well as IntConsumer, DoubleConsumer, and LongConsumer, which accept only Integer, Double, and Long respectively.
Usage
Consumer consumer = (value) -> System.out.println(value); // Or Consumer consumer1 = System.out::println; // Or Consumer consumer2 = (value) -> accept(value);
Example
In this example, we’re using consumer functional interface to print all numbers from a list of integers with the help of a lambda expression and a method reference.
package com.tutorialspoint; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; public class Tester { public static void main(String args[]) { List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8); Consumer<Integer> consumer = (value) -> System.out.println(value); Consumer consumer1 = System.out::println; System.out.println("Printing using consumer functional interface as lambda expression"); numbers.forEach(consumer); System.out.println("Printing using consumer functional interface as method reference"); numbers.forEach(consumer1); } }
Let us compile and run the above program, this will produce the following result −
Printing using consumer functional interface as lambda expression 1 2 3 4 5 6 7 8 Printing using consumer functional interface as method reference 1 2 3 4 5 6 7 8
Supplier Functional Interface
The supplier functional interface is the one whose method does not have any arguments to pass and will return a value. A supplier functional interface is mainly used to generate values lazily. For example, to get a random number, to generate a sequence of numbers, etc.
Usage
Supplier supplier = () -> Math.random() * 10; // or Supplier supplier1 = () -> get();
Example
In this example, we’re using supplier functional interface to get a random number with the help of a lambda expression.
package com.tutorialspoint; import java.util.ArrayList; import java.util.List; import java.util.function.Supplier; public class Tester { public static void main(String args[]) { Supplier<Integer> supplier = () -> (int)(Math.random() * 10); List<Integer> randomNumbers = new ArrayList<>(); // generate 10 random numbers for(int i = 0; i< 10; i++) { randomNumbers.add(supplier.get()); } System.out.println(randomNumbers); } }
Let us compile and run the above program, this will produce the following result −
[0, 8, 8, 8, 8, 5, 7, 5, 5, 9]
Function Functional Interface
Function functional interface is one whose method accepts one argument and will return a value. A function functional interface is mainly used to get the processed value. For example, to get the square of an element, to trim string values, etc. There are other variants of Function as well like BiFunction. A BiFunction functional interface can accept two parameters. Java provides function functional interfaces for primitives as well as IntFunction, DoubleFunction, and LongFunction, which accept only Integer, Double, and Long respectively. There are two more utility interfaces, UnaryOperator which extend the Function interface, and BinaryOperator which extends the BiFunction interface.
Usage
Function function = (value) -> Math.random() * 10; // or Function function1 = (value) -> apply(value);
Example
In this example, we’re using function functional interface to get a list of squares of numbers with the help of a lambda expression.
package com.tutorialspoint; import java.util.Arrays; import java.util.List; import java.util.function.Function; public class Tester { public static void main(String args[]) { List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7,8); Function<Integer, Integer> squared = (value) -> value * value; List<Integer> squaredNumbers = numbers.stream().map(squared).toList(); System.out.println(squaredNumbers); } }
Let us compile and run the above program, this will produce the following result −
[1, 4, 9, 16, 25, 36, 49, 64]
Existing Functional Interfaces Prior to Java 8
With Java, many existing interfaces are annotated as functional interfaces and can be used in lambda expressions. For example:
Runnable − provides run() method
Callable − provides call() method
Actionlistener − provides actionPerformed() method
Comparable − provides compareTo() method to compare two numbers
Example
In this example, we’re creating two threads. First is created using anonymous class and second is using lambda expression. Both are using runnable interface to create the thread instance.
package com.tutorialspoint; public class Tester { public static void main(String args[]) { // create anonymous inner class object new Thread(new Runnable() { @Override public void run() { System.out.println("Thread 1 is running"); } }).start(); // lambda expression to create the object new Thread(() -> { System.out.println("Thread 2 is running."); }).start(); } }
Let us compile and run the above program, this will produce the following result −
Thread 1 is running Thread 2 is running.