The First Solution

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:

FlatMap Solution in Guava

12345

List<Outer>originalList=getListOfObjects();Function<Outer,List<Merged>>flattenFunction//Details left out for clarityIterable<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.