Posted in java on October 23rd, 2013 by Wojciech Soczyński – 6 Comments
As a big fan of the Google Guava library, I was very happy when I’ve got a chance to review this new book from Packt Publishing – “Getting started with Google Guava“. The book has 120 pages of content and is available in several formats from the major internet book stores.
The book covers a lot of a quite big library which Google Guava is. From the basics like Function, Collections and Files to more advanced topics like Concurrency and the Event bus.
The book is of the series “Community experience distilled”. The series name is a good description of the book content. The content is very distilled. Each feature of Guava is explained in a “keyword, short description, example, summary” fashion. This is by no means a weakness of the book – if you have the right attitude. If you are looking for a quick run over the library main features along with code examples, and you want to just scratch the surface, this book is for you. But if you require some more in depth description you will be possibly disappointed.
Personally for me, the book would be a lot better if it could introduce some more context in each chapter describing a part of the library. Speaking of the context I mean i.e in the part about functional programming – what is functional programming, what are the benefits of using it etc. It would be more beneficial for the (assumed) target of the book – novice programmers who want to get some good understanding of Guava. Also the book misses a chapter on good practices – what kind of patterns to avoid,what is especially important in scope of the Collections being the most used part of the library.
While reading the book, the most irritating part of the experience was looking at the example code. In my opinion the samples in the book were just to big and maybe to detailed, often they would span through pages, which doesn’t help when reading.
Enough complaining, what are the good parts ?
The book is concise and a light read – it took me maybe about 2h to read it
It covers a big part of the library
Personally I’ve learned about some things that I didn’t knew
The full code can be used as a copy-paste resource for experimenting with different solutions
Summary – a seasoned developer will be disappointed but the book could be useful for novices.
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.