Sunday 12 March 2023

Understanding Functional Interfaces in Java 8 with Examples

 Introduction:

Functional interfaces are a key feature of Java 8. They enable the use of lambda expressions and method references, which are essential for functional programming in Java. In this article, we will explore what functional interfaces are and how they work in Java 8.


What is a Functional Interface?

A functional interface is an interface that has only one abstract method. An abstract method is a method that does not have an implementation. Functional interfaces are also known as SAM (Single Abstract Method) interfaces.


Functional interfaces are used to represent lambda expressions and method references. They provide a way to define the signature of a lambda expression or a method reference.


Examples of Functional Interfaces:

Java 8 provides several built-in functional interfaces that can be used for different purposes. Here are some examples of built-in functional interfaces in Java 8:


Predicate

The Predicate interface represents a function that takes an argument and returns a boolean value. It is commonly used for filtering elements in a collection.

Here is an example of using a Predicate to filter a list of integers:


List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);

Predicate<Integer> evenPredicate = n -> n % 2 == 0;

List<Integer> evenNumbers = numbers.stream()

.filter(evenPredicate)

.collect(Collectors.toList());


In this code, we define a Predicate that checks if an integer is even. We then use the filter() method to filter out the odd integers from the list.


Consumer

The Consumer interface represents a function that takes an argument and returns no result. It is commonly used for iterating over a collection and performing some action on each element.

Here is an example of using a Consumer to print the elements of a list:


List<String> names = Arrays.asList("Alice", "Bob", "Charlie");

Consumer<String> printConsumer = System.out::println;

names.forEach(printConsumer);


In this code, we define a Consumer that prints a string to the console. We then use the forEach() method to iterate over the list and apply the Consumer to each element.


Function

The Function interface represents a function that takes an argument and returns a result. It is commonly used for transforming elements in a collection.

Here is an example of using a Function to convert a list of strings to a list of integers:


List<String> stringNumbers = Arrays.asList("1", "2", "3", "4", "5");

Function<String, Integer> parseIntFunction = Integer::parseInt;

List<Integer> numbers = stringNumbers.stream()

.map(parseIntFunction)

.collect(Collectors.toList());


In this code, we define a Function that converts a string to an integer. We then use the map() method to apply the Function to each element in the list and convert it to an integer.


Conclusion:

Functional interfaces are a powerful feature of Java 8 that enable functional programming in Java. They provide a way to represent lambda expressions and method references, and make it easier to write concise and expressive code. In this article, we explored what functional interfaces are and how they work in Java 8, with examples of built-in functional interfaces in Java 8.

No comments:

Post a Comment