Let’s replace common list and map usage patterns with Vavr

Jaroslaw Kijanowski
SoftwareMill Tech Blog
4 min readNov 20, 2018

--

Photo by Joao Marcelo Marques on Unsplash

You’ve heard about immutability and all the benefits it provides. From now on you only want to use functional data structures and the first that comes to your mind is obviously a List or a Map. You went through the docs and a tutorial and you’re still not sure how to get your hands dirty? I share some common code snippets I’ve found in various projects and how they could have been replaced with Vavr’s functional data structures:

Very often a private static Map has to be initialized and since Java 9 there is finally a clean solution to this problem. With Vavr it’s done in the same way.

Next:

Iterating over a list to map its values, eventually executing some logic and returning the new collection is quite common. With Vavr the same can be accomplished with less code.

Next:

Another quite common task is to combine all elements into one, for example by concatenating all list elements with a separator. Vavr has provided a dedicated method for that as well as Java.

Next:

For loops to iterate over a list have become extinct. Almost. Sometimes there is still the need to loop over a list and have the current index at hand. Not necessarily to pull the element from the list, but to pass it to some other function together with its index.
Vavr ships zipWithIndex() which accepts a mapper function providing both, the current element and its index.

Next:

Since the introduction of the Optional monadic container, lists may contain not only raw values but wrapped ones into Optional or Option in cases where Vavr is used.
The Java Collections Stream API requires us to filter out empty values via isPresent() and extract them from the wrapper through get(). Not to mention the stream() and collect() boilerplate.
flatMap makes it less verbose. It takes every present value from a given Optional and maps it to a 1-element stream of String. But instead of returning a stream of 1-element streams the result is flattened into one stream containing all the elements. Finally the map() operation can be performed.

Vavr takes it one step further, as usual ;) In this case flatMap extracts the value from the Option wrapper and if the value is not null it is passed further resulting in one list.

Next:

This example shows a case, where the first element of a list is used to do something special like initializing an object. Then the rest of the list is used for a different purpose, here adding skills to that object.
A very similar situation is when you create a WHERE clause in SQL and it has to look like this:
WHERE firstCriteria AND secondCriteria AND thirdCriteria AND fourthCriteria.
The object is initialized with WHERE firstCriteria and the rest is folded to AND xyzCriteria.
With Vavr it’s as simple as using the first element (head()) to initialize the so called zero object and folding the remaining part, the tail().

Next:

This one I googled. How to combine two lists into a map, where the first list forms the keys and the second list provides the values. Stackoverflow came up with some solutions which I wanted to challenge with Vavr.
The zipAll method does the trick, although I’m not really happy to have to restrict the priceList to take just as many elements as the fruit list has. Additionally I need to provide a default value —ignored — in case the price list is longer than the fruits list, which:

  1. technically cannot happen due to the restriction priceList.take(fruits.size()) ,
  2. from a business logic point of view wouldn’t make sense anyway. A fruit without a price is, well, priceless ;) Or ZERO in this particular case. But a price without a fruit does not make much sense.

Still we have to care for this fictional scenario.

Next:

Your turn! Start using Vavr and functional data structures in your projects, be it green field or legacy. Besides all the goodies like performance and thread safety, the API improves readability of your code.

Also make sure you’ve went through the 9 examples on how to replace common if/else statements with Vavr’s Option.

However if you feel like having read yet another post about using the Vavr hammer, where your Java program is supposed to be just a List.of(nails), then you’re ready for a soft reset.

Looking for Scala and Java Experts?

Contact us!

We will make technology work for your business. See the projects we have successfully delivered.

--

--

Java consultant having experience with the Kafka ecosystem, Cassandra as well as GCP and AWS cloud providers. https://pl.linkedin.com/in/jaroslawkijanowski