Consumers and Suppliers on Java 8

I worked with Scala since 2011. Its awesome, its clear to me java 8 copies several ideias from scala but thats fine, i think java 8 is not scala but is cool and its something we can take benefit as jvm users no matter if is coding on java or scala or groovy or jython or jruby.

Java 8 finally bring some functional programming ideias into the JVM. Java 8 its hybrid part OO part functional. Java does functional programming using the constructs and the metaphor around the JVM so they sicked to the current design flow, what it make sense on the jvm design philosophy. (They copy some ideias from Scala, sure).

Right the idea of this posts is talk a little bit on the @FunctionalInterface annotation and some Consumer and Supplier java.util.function.* functions, thats enable us do functional programming in java. Functional programming is java 8 required structure you cannot just start coding things like you do in clojrue or even scala.

Some stuff on the JDK is already ported to @FunctionalInterface and lambdas but not everything and talking about your legacy code? it works, i love backward compatibility in java its the best feature ever for me and the eco-system off course.


This is a special annotation you put on interface to allow they to work with lambdas and have some kinda of Syntactical sugar benefit! Yeah no more inner classes thanks God!

You pretty much mark your normal interface as functional, they allow you to use lambdas in your code. So in this case as you can see i defined a functional interface called Func, Here this is a method called doIt thats dont return anything and dont receive anything as a functional programming example it sucks because if you check my code i`m just doing side effects and its not explicit and thats bad. so i`m just printing hello into the console, besides that you see a lambda () -> thats the main point here just show the constructs.


Suppliers are functions that supply data, like services that provide information, you could think about it as producers, its the same idea, so here i have a day suppliers, it supply the day information. As you can see here i defined a functional interface to do this called Clock but you dont need create a functional interface for everything there is a bunch of default very useful functional interfaces shiped with jdk 8 on the package: java.util.function.* as you can see on the same code with the Supplier interface you see we can have the same result but without the need to create structure thats good.


As you have suppliers(producers) you do have it consumers, they do the opposite they consume information/data and do stuff, we can archive it with functional interfaces or we can use the default structures around java 8.

In this case the consumers consume information and just receive a string and them print it into the console, this 2 interfaces are very useful and allow you to built and and many functional programming compositions around it. And if its not enough you still have use it @FunctionalInterface and create your own abstractions, monads, monoids or everything you might need it.

Full code you can get it here:


Popular posts from this blog

Telemetry and Microservices part2

Installing and Running ntop 2 on Amazon Linux OS

Fun with Apache Kafka