1. Overview
In this tutorial, we’ll provide a quick overview of the Functional Java library along with a few examples.
2. The Functional Java Library
The Functional Java library is an open source library meant to facilitate functional programming in Java. The library provides lots of basic and advanced programming abstractions commonly used in Functional Programming.
Much of the library’s functionality revolves around the F interface. This F interface models a function that takes an input of type A and returns an output of type B. All of this is built on top of Java’s own type system.
3. Maven Dependencies
First, we need to add the required dependencies to our pom.xml file:
<dependency> <groupId>org.functionaljava</groupId> <artifactId>functionaljava</artifactId> <version>4.8.1</version> </dependency> <dependency> <groupId>org.functionaljava</groupId> <artifactId>functionaljava-java8</artifactId> <version>4.8.1</version> </dependency> <dependency> <groupId>org.functionaljava</groupId> <artifactId>functionaljava-quickcheck</artifactId> <version>4.8.1</version> </dependency> <dependency> <groupId>org.functionaljava</groupId> <artifactId>functionaljava-java-core</artifactId> <version>4.8.1</version> </dependency>
4. Defining a Function
Let’s start by creating a function that we can use in our examples later on.
Without Functional Java, a basic multiplication method would look something like:
public static final Integer timesTwoRegular(Integer i) { return i * 2; }
Using the Functional Java library, we can define this functionality a little more elegantly:
public static final F<Integer, Integer> timesTwo = i -> i * 2;
Above, we see an example of the F interface that takes an Integer as input and returns that Integer times two as its output.
Here is another example of a basic function that takes an Integer as input, but in this case, returns a Boolean to indicate if the input was even or odd:
public static final F<Integer, Boolean> isEven = i -> i % 2 == 0;
5. Applying a Function
Now that we have our functions in place, let’s apply them to a dataset.
The Functional Java library provides the usual set of types for managing data like lists, sets, arrays, and maps. The key thing to realize is that these data types are immutable.
Additionally, the library provides convenience functions to convert to and from standard Java Collections classes if needed.
In the example below, we’ll define a list of integers and apply our timesTwo function to it. We’ll also call map using an inline definition of the same function. Of course, we expect the results to be the same:
public void multiplyNumbers_givenIntList_returnTrue() { List<Integer> fList = List.list(1, 2, 3, 4); List<Integer> fList1 = fList.map(timesTwo); List<Integer> fList2 = fList.map(i -> i * 2); assertTrue(fList1.equals(fList2)); }
As we can see map returns a list of the same size where each element’s value is the value of the input list with the function applied. The input list itself does not change.
Here’s a similar example using our isEven function:
public void calculateEvenNumbers_givenIntList_returnTrue() { List<Integer> fList = List.list(3, 4, 5, 6); List<Boolean> evenList = fList.map(isEven); List<Boolean> evenListTrueResult = List.list(false, true, false, true); assertTrue(evenList.equals(evenListTrueResult)); }
Since the map method returns a list, we can apply another function to its output. The order in which we invoke our map functions alters our resulting output:
public void applyMultipleFunctions_givenIntList_returnFalse() { List<Integer> fList = List.list(1, 2, 3, 4); List<Integer> fList1 = fList.map(timesTwo).map(plusOne); List<Integer> fList2 = fList.map(plusOne).map(timesTwo); assertFalse(fList1.equals(fList2)); }
The output of the above lists will be:
List(3,5,7,9) List(4,6,8,10)
6. Filtering Using a Function
Another frequently used operation in Functional Programming is to take an input and filter out data based on some criteria. And as you’ve probably already guessed, these filtering criteria are provided in the form of a function. This function will need to return a boolean to indicate whether or not the data needs to be included in the output.
Now, let’s use our isEven function to filter out the odd numbers from an input array using the filter method:
public void filterList_givenIntList_returnResult() { Array<Integer> array = Array.array(3, 4, 5, 6); Array<Integer> filteredArray = array.filter(isEven); Array<Integer> result = Array.array(4, 6); assertTrue(filteredArray.equals(result)); }
One interesting observation is that in this example, we used an Array instead of a List as we used in previous examples, and our function worked fine. Because of the way functions are abstracted and executed, they do not need to be aware of what method was used to collect the input and output.
In this example, we also used our own isEven function, but Functional Java’s own Integer class also has standard functions for basic numerical comparisons.
7. Applying Boolean Logic Using a Function
In Functional Programming, we frequently use logic like “only do this if all elements satisfy some condition”, or “only do this if at least one element satisfies some condition”.
The Functional Java library provides us with shortcuts for this logic through the exists and the forall methods:
public void checkForLowerCase_givenStringArray_returnResult() { Array<String> array = Array.array("Welcome", "To", "baeldung"); assertTrue(array.exists(s -> List.fromString(s).forall(Characters.isLowerCase))); Array<String> array2 = Array.array("Welcome", "To", "Baeldung"); assertFalse(array2.exists(s -> List.fromString(s).forall(Characters.isLowerCase))); assertFalse(array.forall(s -> List.fromString(s).forall(Characters.isLowerCase))); }
In the example above, we used an array of strings as our input. Calling the fromString function will convert each of the strings from the array into a list of characters. To each of those lists, we applied forall(Characters.isLowerCase).
As you probably guessed, Characters.isLowerCase is a function that returns true if a character is lowercase. So applying forall(Characters.isLowerCase) to a list of characters will only return true if the entire list consists of lowercase characters, which in turn then indicates that the original string was all lowercase.
In the first two tests, we used exists because we only wanted to know whether at least one string was lowercase. The third test used forall to verify whether all strings were lowercase.
8. Handling Optional Values with a Function
Handling optional values in code typically requires == null or isNotBlank checks. Java 8 now provides the Optional class to handle these checks more elegantly, and the Functional Java library offers a similar construct to deal with missing data gracefully through its Option class:
public void checkOptions_givenOptions_returnResult() { Option<Integer> n1 = Option.some(1); Option<Integer> n2 = Option.some(2); Option<Integer> n3 = Option.none(); F<Integer, Option<Integer>> function = i -> i % 2 == 0 ? Option.some(i + 100) : Option.none(); Option<Integer> result1 = n1.bind(function); Option<Integer> result2 = n2.bind(function); Option<Integer> result3 = n3.bind(function); assertEquals(Option.none(), result1); assertEquals(Option.some(102), result2); assertEquals(Option.none(), result3); }
9. Reducing a Set Using a Function
Finally, we will look at functionality to reduce a set. “Reducing a set” is a fancy way of saying “rolling it up into one value”.
The Functional Java library refers to this functionality as folding.
A function needs to be specified to indicate what it means to fold the element. An example of this is the Integers.add function to show the integers in an array or list need to be added.
Based on what the function does when folding, the result can be different depending on whether you start folding from the right or the left. That’s why the Functional Java library provides both versions:
public void foldLeft_givenArray_returnResult() { Array<Integer> intArray = Array.array(17, 44, 67, 2, 22, 80, 1, 27); int sumAll = intArray.foldLeft(Integers.add, 0); assertEquals(260, sumAll); int sumEven = intArray.filter(isEven).foldLeft(Integers.add, 0); assertEquals(148, sumEven); }
The first foldLeft simply adds all the integers. Whereas the second will first apply a filter and then add the remaining integers.
10. Conclusion
This article is just a short introduction to the Functional Java library.
As always, the full source code of the article is available over on GitHub.