Tuesday, April 24, 2018

VAVR - Using map(), flatMap(), Option and Try to get different return types

Simple map() operation in vavr.io takes as argument a function that has as a parameter the type of element contained in the list. The return type of the method in the function can be anything we want. The purpose of map is to transform from one type to another.

public List<BigDecimal> simpleMap(List<Integer> numbers) {
      return numbers.map(n -> m1(n));

    private BigDecimal m1(Integer i) {
     return new BigDecimal(i);

flatMap() uses the same mechanics as map but the only difference is that it will remove the duplication by collapsing the duplicates into a single entry. e.g 1,2,2,2,3 flatMapped will become 1,2,3
public List<BigDecimal> flatMapping(List<Integer> numbers) {
    return numbers.flatMap(n -> m2(n));

   private List<BigDecimal> m2(Integer i) {
     return List.of(new BigDecimal(i));

Sometimes a function can return List<Try<Option<?>>>> that is fine but perhaps Option is sometimes redundant. Notice that this method uses Try<Option>, that looks a bit overkill
public List<Try<Option<String>>> returningARedundantOption(List<Integer> numbers) {
        return numbers.map(n -> m3(n));

    private Try<Option<String>> m3(Integer i) {
        //Imagine this option is the result of intereacting with other code
        // e.g some dao object
        return Try.success(Option.some(""));

To solve the redundancy shown in the example above, we can perform an additional flatMap() so that we get rid of the Option by mapping it to a Try using the toTry() method inside Option. This way we get a List<Try<String>>.
public List<Try<String>> removingRedundancy(List<Integer> numbers) {
        return numbers.map(n -> {
            return m3(n).flatMap(Option::toTry);
    //Same as above
    public List<Try<String>> removingRedundancy(List<Integer> numbers) {
        return numbers.map(n -> m3(n).flatMap(Option::toTry));

    private Try<Option<String>> m3(Integer i) {
        return Try.success(Option.some(""));

In this final example we map a set of integers to a Try<Option<String>> and then we flatMap the result to Set<Try<String>> in order to transform that Set<Try<String>> into a Try<List<String>> we pass the result to Try.sequence() and we map the outcome to list.
public Try<List<String>> usingSequence(Set<Integer> ids) {
        Set<Try<String>> result = ids.map(id -> m4(id).flatMap(Option::toTry));
        return Try.sequence(result).map(Seq::toList);

    //Same as above
    public Try<List<String>> spike2(Set<Integer> ids) {
        return Try.sequence(ids.map(id -> m4(id).flatMap(Option::toTry))).map(Seq::toList);

    private Try<Option<String>>  m4(Integer id) {
        Try.success(Option.of("something" + id));

For more information about the vavr.io framework: http://www.vavr.io/

Wednesday, August 23, 2017

Dockerizing a modern Java application

In this video I show the structure of a modern Java web application that uses Spring boot, Spring mvc, gradle, angularjs and then I will show how it is commonly prepared to run from within a docker container.

Full Source codes: https://github.com/SFRJ/tictactoe

Thursday, April 20, 2017

Browser automation in Java using Cucumber and Selenium

This is video I created to explain some of the very basics of browser automation in Java. Using the the classic tools Cucumber and Selenium.

Thursday, April 13, 2017

Implementing Strategy Design Pattern(Practical example)

In this video I show how to refactor some ugly if-else nested statements using polymorphism through the strategy design pattern.

Please note that I didn't use code coverage in this occasion, this was more of an Spike than a refactor I would say. My goal was to just show the implementation of the pattern and make a brief video. Of course if you are trying this in real software, please make sure that your code is covered by unit tests before proceeding.

Tuesday, April 11, 2017

Checking that 2 Lists have elements in common

How many times have we created loops to check if any of the elements of one list are contained among any of the elements of other list?

Lists have a contains() and also a containsAll() methods but they doesn't seem to do what we want and we end up writing spaghetti that looks like this often:

1:  boolean isSafeToEat(List<String> allergicFoods) {  
2:    for (String ingredient : getIngredients()) {  
3:      if (allergicFoods.contains(ingredient)) {  
4:        return false;  
5:      }  
6:    }  
7:    return true;  
8:  }  

Sometimes we forget that the Java API has so many useful things. Like this disjoint() method, from Collections.

1:    boolean isSafeToEat(List<String> allergicFoods) {  
2:      return Collections.disjoint(ingredients, allergicFoods);  
3:    }  

Its not Java 8 ;) its being there for a bit already, have a look at this docs:

Refactor switch to Enum

Do switch statements sound good to you? 
Trust me better switch to Enum before it destroys your ears ;)

Monday, April 10, 2017

Mocking static method calls

Very simple example on how we can use method delegation to enable unit testing where there is a dependency to an static method call.

Share with your friends