1. The purpose of this post

I would demo how to return multiple result objects from a java method by using Vavr.

1.1 What is Vavr

According to the Vavr’s website:

Vavr is an object-functional language extension to Java 8, which aims to reduce the lines of code and increase code quality. It provides persistent collections, functional abstractions for error handling, concurrent programming, pattern matching and much more.

Because Vavr does not depend on any libraries (other than the JVM) you can easily add it as standalone .jar to your classpath.

2. Environments

  • java 1.8+
  • Vavr 0.9+

3. How to return multiple objects from a java method

3.1 The old way

If we want to return a result that contains three objects , we can define a result object like this:

    class TempResult {
        String name;
        int id;
        List<String> props;
    }

And then we return it in the method like this:

    private TempResult getResults() {
        TempResult result = new TempResult();
        result.name = "test";
        result.id = 1;
        result.props = Arrays.asList("1","2","3");
        return result;
    }

Then, we consume the result like this:

        TempResult tempResult = getResults();
        System.out.println(String.format("name %s id %d props %s",
                tempResult.name,
                tempResult.id,
                tempResult.props.stream().reduce("",String::concat)));

Run the code, we get this:

name test id 1 props 123

3.2 The Vavr way

3.2.1 Add Vavr dependency to your pom

        <dependency>
            <groupId>io.vavr</groupId>
            <artifactId>vavr</artifactId>
            <version>0.10.0</version>
        </dependency>

You can get vavr latest version from link

3.2.2 Use the Tuple from Vavr

Tuples are immutable and can hold multiple objects of different types in a type-safe manner.

Let’s redefine the getResults by using vavr’s tuple:

	import io.vavr.Tuple3;

	....

    private Tuple3<String,Integer,List<String>> getResultsByTuple() {
        return new Tuple3<>("test",1,Arrays.asList("1","2","3"));
    }

It’s very simple:

  • You can see that we use Tuple3, that means this tuple contains three objects, Tuple1 means only one objects in the tuple
  • Because Tuple is immutable, so you should use the constructor to build the Tuple, not by setters.

Then we use the Tuple result and print it:

		Tuple3<String,Integer,List<String>> result = getResultsByTuple();
        System.out.println(String.format("name %s id %d props %s",
                result._1,
                result._2,
                result._3.stream().reduce(" ",String::concat)));

You can see that:

  • We use the Tuple’s _1,_2,_3 to access the objects stored in the Tuple, it’s very simple
  • Instead of using _1 to access objects, you can also use _1() to access the number one object stored in the Tuple

Run the code, we get this:

name test id 1 props 123

It’s just the same as the old way.

4. Summary

Vavr’s Tuple is a good way to encapsulate temporary immutable objects, it’s not necessary to define such objects that only be used once.

To be continued on this topic…