If you’re a Java developer, I’m sure that you have seen code similar to the featured image snippet above at least once. The code in the snippet above is an example of functional programming paradigm implementation in Java, which will filter and transform the in the request to another . List<String> List<String> In this article, I will write about how to write code using Java’s API for functional programming. In the end, we will write our own stream API so we can understand how to implement a functional programming style in Java. Functional Programming in Java Functional programming in Java has been around for a long time. When Oracle released Java 8 back in 2014, they introduced , which was the core feature for functional programming in Java. lambda expression Let’s see an example of the difference between using a sequence of imperative statements and using a functional style in Java. Imperative declaration code example: Functional declaration code example: As we can see, even though both pieces of code achieve the same result, the difference is significant. The imperative declaration code has many curly braces and is much longer, which makes it harder to read, compared to the functional style code. Functional Interface Annotation To understand how functional programming works in Java, first we will need to look at the annotation included in Java 8 SDK, . We can look at it on the . @FunctionalInterface Java API documentation site From the API documentation, we can see that the behaviors of a functional interface annotation in Java are: It has exactly one abstract method in it. It can have more than one method, as long as there is only one abstract method. We can only add it to type. Interface We can create the functional interface with a lambda expression, method references, or constructor references. We don't need to define because the compiler will treat any interface meeting the definition of a functional interface as a functional interface. @FunctionalInterface Now we know what a functional interface all about, we can create it by ourselves. Let’s first create a model called . Person For the functional interface, we’ll create class. PersonFunctionalInterface Note that there are two methods in the interface, but since there is only one abstract method, class is valid as a functional interface. PersonFunctionalInterface But suppose we define more than one abstract method, like so: It will produce an error: Using a Functional Interface Anonymous class Let’s first learn about the anonymous class. Java says that: documentation “Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name. Use them if you need to use a local class only once.” Basically, with an anonymous class, we don’t have to define a class that implements the interface we made. We can create a class without a name and store it in a variable. Let’s declare an anonymous class as an example. What we’ve done here is we created an anonymous class with type and name. PersonFunctionalInterface anonClassExample We override the abstract method so when we call the method, it will return a new Person object with a name. createPerson When we called , we basically just created a new Person object with “Hello, World” as its name. anonClassExample.createPerson(“Hello, World”) Creating an Anonymous Class With a Functional Interface We can start creating the anonymous class of for the functional interface we made. PersonFunctionalinterface We’ve just implemented the functional interface! In the code above, we created three anonymous classes in different ways. Remember that the anonymous class has the behavior that we can create a functional interface with a lambda expression, method references, or constructor references. To make sure we created anonymous classes that behave the same, we assert every method in the interface. Built-In Functional Interface in Java 8 Java 8 has many built-in functional interface classes in the package that we can see in . java.util.function its documentation In this article, I will only explain four of the most commonly used functional interfaces, but if you’re interested in more, you can read it in the Java API documentation noted above. : A functional interface that accepts an object and returns nothing. Consumer<T> : A functional interface that accepts nothing and returns an object. Producer<T> : A functional interface that accepts an object and returns a boolean. Predicate<T> : A functional interface that accepts an object and returns another object. Function<T, R> Common Usage If you’ve been developing with Java a lot, then it’s likely you’ve met the concept of functional interface already. Stream and optional API Java’s Stream API uses functional interfaces a lot, as we can see in the code below. The method has a parameter functional interface. As we can see, the method accepts a and produce a . filter Predicate<T> String boolean The method uses as its parameter. It accepts a String and also returns . map Function<T, R> String The method in Stream and method in Optional accept , accepting a and not returning anything. forEach ifPresent Consumer<T> String Reactive library Both of the most popular Java Reactive libraries, and , are based on Java 8 Streams API, which means they also use functional interfaces in their code. RxJava Reactor If we look at and , we can see many of their methods accept a functional interface. Reactor’s Flux API documentation RxJava’s Observable API documentation Creating Our Own Stream API Now that we know how to create and use a functional interface, let’s try creating our own streaming API so we can understand how we can implement the functional interface. Of course, our streaming API is much simpler than Java’s. And a test class: Okay, let’s discuss the methods one by one. Constructor We made two constructors, one constructor imitating the API and one constructor to convert to . Stream.of() List<T> SimpleStream<T> Filter In this method, we accept as a parameter since has an abstract parameter named that accepts an object and produces a . Predicate<T> Predicate<T> test boolean Let’s look at the test class, where we wrote: This means we wrote an anonymous class implementing : Predicate<T> So in the class, we can see the filter method as: SimpleStream<T> Map In the map method, we accept as its parameter, which means the map method will accept a functional interface that accepts an object and also produces an object. Function<T, R> We wrote the following in the test class It’s the same as creating an anonymous class implementing : Function<T, R> And in the class, we can see it as this: SimpleStream<T> forEach The method accepts as its parameter, meaning that it will accept an object and return nothing. forEach Consumer<T> We wrote the following in the test class: This translates to creating an anonymous class implementing : Consumer<T> In the , we can see the method, as below: SimpleStream<T> forEach Conclusion With the release of Java 8 back in 2014, we can use a functional programming style in Java. Using a functional programming style in Java has many benefits, one of which is making your code shorter and more readable. With the benefits it provides, knowing the implementation of functional programming in Java if you’re a Java developer is a must! Thanks for reading this article! You can find the GitHub repository used for this article here: https://github.com/brilianfird/java-functional-programming Resources https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html . https://www.amitph.com/java-method-and-constructor-reference/#:~:text=Constructor%20Reference%20is%20used%20to,assign%20to%20a%20target%20type https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html http://reactivex.io/RxJava/javadoc/ https://projectreactor.io/docs/core/release/api/ Previously published at https://codecurated.com/blog/functional-programming-in-java-explained/