[Google Guava] – One class to rule’em all – FluentIterable

Google Guava has a lot of interesting utility classes, just to name a few: Optional, Joiner, Splitter, EventBus, Collections2 etc. But the essence of the whole library is condensed in just one single class – FluentIterable.

The FluentIterable class let’s you combine a set of operations on a collection in a single pipeline, plus it has some nifty methods too :).

To start a pipeline, you should use the simple FluentIterable.from(collection) method:

Then you can perform typical collection operations. For the purpose of the example, let assume that we want to square all even numbers from 1 to 100 and select from the result the first number that is greater than 17 and smaller than 50.

Note that for a range comparison I have used the Guava Range class which is a cool utility for dealing with ranges. One can also replace the “generateInput” method with a combination of a ContiguousSet and Range invocations:

The last but not the least interesting method in the FluentIterable is the transformAndConcat method. It allows to perform a transformation of a collection of some objects and merge the outputs, assuming that the result of a single item transformation was also a collection. This is very handy for situations like making a flat structure from some kind of Tree or a nested List. A common example is a list of files in a tree structure of the file system.

Note in the above example, that I have used an “identity function”. The identity function is a function that simply returns its input.

We are near the end, so for the close up I would like to tell a few words about performance. I have done some tests using the Google Caliper benchmarking library. The case was about squaring every item of a collection which was an even number. I have made two implementations:

…a vanilla Java one:

… and a Guava based one:

The test:

The test was run 10 times (10 reps) for 20 trials. On the average the Guava solution took about 1,5x time of the Java one.

All in all I think that the performance penalty is not very big and it is beneficial to use the FluentIterable class for the sake of coding pleasure, readability and testability.

 

 

 

 

  1. s.coop says:

    There are numerous reasoned explanations why individuals still cannot choose using BlueStacks or virtually any Android os emulator pc software.
    This might be because they don’t really progress user experience
    while using the them which is mainly as a result of compatibility problems.

  2. Every hack from this hack tool doesn’t fail to make its users more contented than ever before.
    These cheats help users to improve the life time plus the general power of these
    snake.

  1. There are no trackbacks for this post yet.

Leave a Reply

Notify me of followup comments via e-mail. You can also subscribe without commenting.