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 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.
Most PHP developers, that start learning Java face the same shocking issue – Java does not have arrays ! Well it is not completely true, Java has a notion of arrays, but not it the sense that most PHP developers are familiar to.
In PHP, the array is an universal container. It can be used as a map:
and as a list:
In PHP arrays are used everywhere, because they are very convenient. There are plenty of awesome functions that operate on arrays, to name just a few:
The last cool feature of a PHP array is that, it remembers the order in which the elements where added.
In Java, things are slightly different. There are several collection types:
array – not actually a collection in Java sense, it has a fixed length (similar to SplFixedArray)
Map – a typed hash map (similar to PHP’s array, when we use string’s as keys), similar to SPLObjectStorage
List – a typed list – a container which remembers the order in which the items was added, similar to SPLDoublyLinkedList
Set – a typed collection of unique items
Every collection type is iterable (which means that you can use the for-each loop to iterate over it). All collection types derive from the base abstract Collection class.
The types that I have mentioned above are just interfaces, every one of it has different implementations which have some slightly different properties:
Map – implemented by HashMap and LinkedHashMap, the LinkedHashMap retains the insertion order
List – implemented by ArrayList and LinkedList, both implementations differ mainly in the speed of the operations – the ArrayList is faster for reading and the LinkedList is faster for writing
Set – implemented by HashSet, TreeSet and LinkedHashSet – the LinkedHashSet retains the insertion order, the TreeSet is sorted and the HashSet is the fastests
To even further complicate the notion of collections in Java, every collection type has it’s immutable form .
To sum it all up, transitioning from PHP to Java in case of Arrays is not that difficult, you must just resolve which collection type you should use in each case. I will show some examples in the next post of this series, so stay tuned.
Hi folks ! There was some time ago when I was actively blogging (half of the year or so), so I felt a bit ashamed and decided to write about something interesting. As you probably know I’m working now as a Java developer. Because I’m not a big fan of Java (actually I do hate it) I look from time to time for solutions to over-go Java’s shortcomings.
For some time I was eager to write some sample application that will be using Scala and the Akka actors framework. The main problem was to pick up an example, that would make sense to apply both technologies. The idea for the example came to me all of sudden, when I was sitting with my friends after a soccer game. Usually, when you play a game, you are talking about it right after it ends, but this time it was different. My colleagues were talking about poker !