Archive

Posts Tagged ‘collections’

Why should you use Google Guava Libraries in your next project

January 27th, 2015 No comments

We have used google guava libraries heavily in our project and here are some core reasons why you should use guava libraries. Guava is open source library in java written by google. These are used by google so you can pretty much rely on them in your projects.

Why you should use Google Guava

An easy to use , out of the box libraries used by google engineers. Come with a lot of standard features which are missing in java. Since they have been used at google, you can be rest assured that they have thought about performance. I really like the useful functions they have build around collections like having mutable / immutable collections, supporting multikey map, static constructors.

Google calls the guava libraries as  productivity multiplier and I agree with that.

What we have used in Google Guava

Static Constructors

before JDK 7 , we would create a new constructor like

<br /><br />List cityList = new ArrayList();<br /><br />

After jdk7 , this has changed to

<br /><br />List cityList = new ArrayList&lt;&gt;();<br /><br />

Still I do not want to use new word. So in Guava, you can create using static constructor

List cityList = Lists.newArrayList();

Predicate

Determines a true or false value for a given input. More on predicate here Using predicates

Iterables

Look at this example on how you can iterate , and use predicate  to find out what

Set<>{

for (Target target : Iterables.filter(targets,
Predicates.not(new ConflictTargetPredicate()))) {
//do something
}

Multimap

Multimap in guava is similar to a Map, but you can associate multiple values with a single key. If you call put (K,V) twice, with the same key but different values, the multimap contains mappings from the key to both values.  So when you do a get(K),  A Collection<V) is returned.

Collections2

Renamed as Collections2  so that it does not conflict with java collections. Some of the major features in collections2 are

Immutable Collections

Immutable objects have many advantages, including:

  • Safe for use by untrusted libraries.
  • Thread-safe: can be used by many threads with no risk of race conditions.
  • Doesn’t need to support mutation, and can make time and space savings with that assumption. All immutable collection implementations are more memory-efficient than their mutable siblings (analysis)
  • Can be used as a constant, with the expectation that it will remain fixed

Making immutable copies of objects is a good defensive programming technique. Guava provides simple, easy-to-use immutable versions of each standard Collection type, including Guava’s own Collection variations.

JDK also provides collections to be immutable with Collections.unmodifiableXXX methods. Guava mentions that this is not good because

  • unwieldy and verbose; unpleasant to use everywhere you want to make defensive copies
  • unsafe: the returned collections are only truly immutable if nobody holds a reference to the original collection
  • inefficient: the data structures still have all the overhead of mutable collections, including concurrent modification checks, extra space in hash tables, etc.

When you don’t expect to modify a collection, or expect a collection to remain constant, it’s a good practice to defensively copy it into an immutable collection.

Sets.newHashSet() : Creates a mutable empty hashSet instance

Lists.newArrayList() : Creates a mutable arraylist

Convert Set to a List

Handy to convert  set to list without declaring a new object.

</pre>
<code>Lists.newArrayList([yourSet])</code>

Functional style programming

</pre>
Collection<?> myNotNullsCollection = filter(myCollection, notNull());

This will remove elements which are null and return a new copy. Remember this will not modify the existing copy. A really handy feature.

Caches

A very useful feature to store data for short term. helps in improve performance at expense of some memory. A use case is let say you have to calculate interest rates for all days in a  year and you need to do that 200 times.