Streams, Map, Reduce and Filter on Java 8

For me this is one the most cool things java 8 got in sense of functional programming.

This allow todo real functional programming with great benefit for legacy code, not only because we got modern constructs but also because you can reduce a lot the lines of code.

The things i dont like in java 8 is that we still need some kinda of structure / definition thats a little bit heavy or not that natural like it is in scala or clojure but thats is something i guess we can live with it.  The other thing is the fact that immutability is not forced as much as we see in other languages the reasons are kinda of obvious it because java needs to respect they backward compatibility rules, whats great because my code can keep ruining into java 8 without any change or a i can embrace this "new" ideas and functional concepts and start taking benefits from it.

The Code

There are plenty of things on this code but her we go. So you can see we are using a collection called numbers, in java collections extend a new interface called Stream, this is the same concept we have in clojure and scala called sequence. This allow functions and functors to work and normal or lazy mode on collections and data in a sequential or parallel way.

The Map Function

this is my favorite function in FP. with it will stop a lot using FOR and IF,thats a great smell for those who work with scala and clojure as less FOR, IF and variables declarations you do probably the better it means for functional you code could get. 

So the map function(from Stream) you work on the data that you collection holds like: arrays, vectors, lists, etc... and them you pass a lambda function that will receive 2 parameters and return data. In this maps here i an doing an increment. So if the collection is (1,2,3) will become (2,3,4),

The Filter + Predicate

Filter allow us to filter data and just select / view the parts we want, it works with predicates so my predicates on the sample are checking if the numbers are odd and dropping the even numbers. 

The Reduce

This is my second favorite function. This function will consume and destroy the actual collection in the way it will receive a lambda with 2 arguments. hold on a sec this is exactly like map, yes its similar parameter but totally different behavior. Maps have an expansive property will reduce has and consumer/destructive property so, in this cases here reduce will get each element of the collection and apply to a function and pass the accumulator and the next element so as you can see here i`m just doing a sum of all elements. 

The Output

When you run this code you gonna get this:

So the first stream, iterates all the numbers and increment them all them later just print the result. The second stream filter just the odd numbers and sum the all in the and the last one to the same as the second one but with the numbers that are divisible by 7(mod 7) since we have Nome we got and empty result. 

The great things about Stream is that they do with Monads, in java we have the monad Optional they same as Option[T] in scala or getOrElse in Haskell this is great because avoid and will never ever get an NullPointerException.


Popular posts from this blog

Telemetry and Microservices part2

Installing and Running ntop 2 on Amazon Linux OS

Fun with Apache Kafka