Over a million developers have joined DZone.

FlatMap in Guava

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

This is a short post about a method I recently discovered in Guava.

The Issue

I had a situation at work where I was working with objects structured something like this:

public class Outer {
    String outerId;
    List<Inner> innerList;
    .......
}

public class Inner {
    String innerId;
    Date timestamp;
}

public class Merged {
    String outerId;
    String innerId;
    Date timestamp;
}

My task was flatten a list Outer objects (along with the list of Inner objects) into a list of Merged objects. Since I’m working with Java 7, using streams is not an option.

The First Solution

Instead I turn to the FluentIterable class from Guava. My first instinct is to go with the FluentIterable.transform method (which is essentially a map function):

List<Outer> originalList = getListOfObjects();

Function<Outer,List<Merged>> flattenFunction //Details left out for clarity

//returns an Iterable of Lists!
Iterable<List<Merged>> mergedObjects = FluentIterable.from(originalList).tranform(flattenFunction);

But I really want a single collection of Merged objects, not an iterable of lists! The missing ingredient here is a flatMap function. Since I’m not using Scala, Clojure or Java 8, I feel that I’m out of luck.

A Better Solution

I decide to take a closer look at the FluentIterable class and I discover the FluentIterable.transformAndConcat method. The transformAndConcat method applies a function to each element of the fluent iterable and appends the results into a single iterable instance. I have my flatMap function in Guava! Now my solution looks like this:

List<Outer> originalList = getListOfObjects();

Function<Outer,List<Merged>> flattenFunction //Details left out for clarity

Iterable<Merged> mergedObjects = FluentIterable.from(originalList).transformAndConcat(flattenFunction);

Conclusion

While this is a very short post, it goes to show how useful the Guava library is and how functional programming concepts can make our code more concise.



Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Published at DZone with permission of Bill Bejeck, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}