The Artima Developer Community
Sponsored Link

Artima Developer Spotlight Forum
Simplifying Java Code with Google Collections, Guava, and Static Imports

1 reply on 1 page. Most recent reply: Oct 16, 2009 8:27 AM by Daniel Jimenez

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Frank Sommers

Posts: 2642
Nickname: fsommers
Registered: Jan, 2002

Simplifying Java Code with Google Collections, Guava, and Static Imports Posted: Oct 13, 2009 8:56 PM
Reply to this message Reply
Advertisement

Java developers frequently complain that many common programming tasks in Java require a copious amount of code. While some seek concision in the land of alternate, more succinct JVM languages, others aim to stretch Java's existing language features to support API-level solutions to verbosity.

Two recent examples of the API-based approach are Google's Collections and Guava libraries. In a recent blog post, Beautiful Code with Google Collections, Guava and Static Imports Aleksander Stensby illustrates how these libraries can make Java programming a more rewarding experience:

Guava is still a rather immature library that is subject to changes over the coming months... Google Collections will become part of Guava once Collections reaches it’s 1.0 release. Guava (and Google Collections) have been pioneered by several Google developers and is widely used in their “myriad of Java projects...”

You can benefit from using Guava with Collections to reduce the amount of boilerplate code in your projects (and gain access to the new and faster data structures that they provide!).

As an example, Stensby shows how to create collection instances with Google Collections:

Map<String, Map<Long, List<String>>> map = new HashMap<String, Map<Long,List<String>>>();

Map<String, Map<Long, List<String>>> map = Maps.newHashMap();

Map<String, Map<Long, List<String>>> map = newHashMap();

Thanks to generics and those handy factory methods that the Collections guys have provided us with, we no longer have to write things that Java really should understand, right? ... I know this will be part of JDK 7, and that’s great. But Google Collections is here, now...

Similar to the static utility methods that com.google.common.collect.Maps provide, we also have Lists and Sets...

Lists.newArrayList();
Sets.newHashSet();

Stensby also demonstrates how to initialize a collection with some values:

ImmutableList<String> of = ImmutableList.of("a", "b", "c", "d");

ImmutableMap<String,String< map = ImmutableMap.of("key1", "value1", "key2", "value2");

Apart from the handy and clean ways of creating collections and populating them, we are also provided with lots of additional utility methods like filtering, set intersections and union, ordering and even some neat functional stuff!

In the second part of his blog post, Stensby turns to the Guava library:

Guava provides us with a comprehensive extension to the Core Java Libraries. Among other things we gain access to utilities for working with primitives through the Ints, Doubles, Floats, Shorts, Bytes and Bools classes in the com.google.common.primitives package. The com.google.common.io package provides utilities for working with streams, buffers, files etc, and the concurrent package provides classes like Futures, Callables and Executors to ease the pain of writing concurrent code. In addition to all of this, Guava also provides us with some additions to Collections and a really neat CharMatcher class, Joiner and Splitter classes.

What do you think of Google's Collections and Guava libraries as tools to help reduce boilerplate Java code?


Daniel Jimenez

Posts: 40
Nickname: djimenez
Registered: Dec, 2004

Re: Simplifying Java Code with Google Collections, Guava, and Static Import Posted: Oct 16, 2009 8:27 AM
Reply to this message Reply
So, I like this. I use static functions and static imports for cleanup quite a bit. I used to write abstract classes with protected-static-final functions and protected constructors just to remove one extra name+dot when dereferencing function calls, and this is much cleaner.

I also like naming these function-bundles (they're not really classes, sort of) Thing+s instead of ThingUtils.

These are pretty much our only mechanisms for "extending" primitives / final classes / the jdk lang/util/io packages, huh? No one has come up with any other solutions to this?

(Yes, I know, use Scala. :-)

Flat View: This topic has 1 reply on 1 page
Topic: Java is Dead, But You'll Learn to Love It Previous Topic   Next Topic Topic: Noop: A JVM Language to Encourage the Good, Discourage the Bad

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use