Java Functional Interfaces For the Impatient: Part 1

In this article, we will learn about Java Functional Interfaces with coding examples. This is just part one of the series as I will try to cover the main functional interfaces which come under Package java.util.function.Image by fullvector on FreepikIn…


This content originally appeared on Level Up Coding - Medium and was authored by Ashish Choudhary

In this article, we will learn about Java Functional Interfaces with coding examples. This is just part one of the series as I will try to cover the main functional interfaces which come under Package java.util.function.
Image by fullvector on Freepik

Introduction

Functional Interfaces were first introduced in Java 8 and as per Java docs:

Functional interfaces provide target types for lambda expressions and method references. Each functional interface has a single abstract method, called the functional method for that functional interface, to which the lambda expression’s parameter and return types are matched or adapted.

In Java, a functional interface is a type of interface that represents a single functionality, or action, that can be performed in your program. Think of it as a recipe describing how to perform a specific action, like baking a cake. The recipe gives step-by-step instructions on what to do but doesn’t do the action for you. Similarly, a functional interface in Java defines a method or set of methods describing a specific action your program can perform.

Functional interfaces are helpful because they can simplify and streamline your code. For example, let’s say you have a program that needs to perform many similar actions, like adding two numbers together or converting a string to uppercase. Instead of writing out the code for each action separately, you can define a functional interface that describes the action and then use that interface to act as many times as needed.

Why were Functional Interfaces added to Java?

Functional interfaces were added to Java to support better a programming style called functional programming, which emphasizes using functions as objects that can be passed around and used as inputs or outputs to other functions. In other words, you can treat a function as a variable, pass it as an argument to other methods, or assign it to a variable, just like you would with a regular value. This makes writing more modular and flexible code easier, which can be easier to read and maintain.

Before Java 8, creating functions in Java was more complicated and required more code, which made functional programming in Java less intuitive and efficient.

Classification

Functional Interfaces under java.util.function package can be broadly classified into the following.

  1. Consumer <T>
  2. Function<T,R>
  3. Supplier<T>
  4. Predicate<T>

Consumer<T> Functional Interface

A Consumer is a functional interface with a single abstract method accept(Object), which allows only one argument and returns no results.

import java.util.function.Consumer;

public class ConsumerExample {
public static void main(String[] args) {
Consumer<String> printValue = s -> System.out.println(s);
printValue.accept("Hello World");
}
}

Predicate<T> Functional Interface

A Predicate is a functional interface representing a function that takes one argument and returns a boolean value. It is commonly used for filtering, testing, and validation of objects.

import java.util.function.Predicate;

public class PredicateExample {
public static void main(String[] args) {
Predicate<String> stringLength = s -> s.length() > 5;
System.out.println(stringLength.test("Hello World")); //true
System.out.println(stringLength.test("Java")); //false
}
}

Function<T,R> Functional Interface

A Function is a functional interface representing a function that takes one argument and returns a value. It is frequently used for mapping or transforming input values to output values.

import java.util.function.Function;

public class FunctionExample {
public static void main(String[] args) {
Function<String, Integer> stringToInt = s -> Integer.parseInt(s);
System.out.println(stringToInt.apply("10")); //10
}
}

Supplier<T> Functional Interface

A Supplier is a functional interface representing a function that takes no arguments and returns a value. It is typically used for lazy evaluation of objects or for generating values.

import java.util.function.Supplier;

public class SupplierExample {
public static void main(String[] args) {
Supplier<String> helloSupplier = () -> "Hello World";
System.out.println(helloSupplier.get());
}
}

Conclusion

Functional interfaces are a way of describing specific actions that your program can perform and then using those descriptions to simplify and streamline your code. They’re a powerful tool to help you write more efficient and flexible code. By mastering these interfaces, developers can create more efficient, concise, and effective code.

Further Reading

Docker Tutorial Series: Part 1

docker tutorial for beginners

Docker Tutorial Series: Part 2

Docker installation

Level Up Coding

Thanks for being a part of our community! Before you go:

🚀👉 Join the Level Up talent collective and find an amazing job


Java Functional Interfaces For the Impatient: Part 1 was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Ashish Choudhary


Print Share Comment Cite Upload Translate Updates
APA

Ashish Choudhary | Sciencx (2023-02-21T17:32:54+00:00) Java Functional Interfaces For the Impatient: Part 1. Retrieved from https://www.scien.cx/2023/02/21/java-functional-interfaces-for-the-impatient-part-1/

MLA
" » Java Functional Interfaces For the Impatient: Part 1." Ashish Choudhary | Sciencx - Tuesday February 21, 2023, https://www.scien.cx/2023/02/21/java-functional-interfaces-for-the-impatient-part-1/
HARVARD
Ashish Choudhary | Sciencx Tuesday February 21, 2023 » Java Functional Interfaces For the Impatient: Part 1., viewed ,<https://www.scien.cx/2023/02/21/java-functional-interfaces-for-the-impatient-part-1/>
VANCOUVER
Ashish Choudhary | Sciencx - » Java Functional Interfaces For the Impatient: Part 1. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/21/java-functional-interfaces-for-the-impatient-part-1/
CHICAGO
" » Java Functional Interfaces For the Impatient: Part 1." Ashish Choudhary | Sciencx - Accessed . https://www.scien.cx/2023/02/21/java-functional-interfaces-for-the-impatient-part-1/
IEEE
" » Java Functional Interfaces For the Impatient: Part 1." Ashish Choudhary | Sciencx [Online]. Available: https://www.scien.cx/2023/02/21/java-functional-interfaces-for-the-impatient-part-1/. [Accessed: ]
rf:citation
» Java Functional Interfaces For the Impatient: Part 1 | Ashish Choudhary | Sciencx | https://www.scien.cx/2023/02/21/java-functional-interfaces-for-the-impatient-part-1/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.