Shared publicly  - 
 
I just wrote a whole bunch of code with Iterables that could have been much better with this instead. Time for some cleanup!
 
Guava 12.0 introduces FluentIterable, a new and improved way of doing many of the things you can already do with the Iterables class. Instead of static methods for transforming, filtering and querying, FluentIterable wraps another Iterable (or is extended directly) and provides instance methods for doing those same things. The methods that create views return FluentIterable themselves, allowing you to chain calls. The result is much more readable code when you have a series of operations you want to apply in a row. Just look at the difference in the following equivalent examples.

Before:

Iterable<Dessert> desserts = ...
ImmutableSet<Filling> pieFillings = ImmutableSet.copyOf(
. . Iterables.transform(
. . . . .Iterables.limit(Iterables.filter(desserts, Pie.class), 10)
. . . . .Pie.GET_FILLING_FUNCTION));

After:

ImmutableSet<Filling> pieFillings = FluentIterable.from(desserts)
. . .filter(Pie.class)
. . .limit(10)
. . .transform(Pie.GET_FILLING_FUNCTION)
. . .toImmutableSet();

With FluentIterable, the code reads from top to bottom rather than inside out and it’s much easier to see what the arguments to each method are. While you should still take care not to overuse Guava’s functional APIs where they aren’t needed (see http://code.google.com/p/guava-libraries/wiki/FunctionalExplained#Caveats), FluentIterable goes a long way to make the places you use it more readable.

-- +Colin Decker, Guava contributor and future team member
1
Add a comment...