Google Guava vs. Apache Commons

68,444

Solution 1

In my opinion the better choice is Guava (formerly known as Google collections):

  • it's more modern (has generics)
  • it absolutely follows the Collections API requirements
  • it's actively maintained
  • CacheBuilder and it's predecessor MapMaker are just plain awesome

Apache Commons Collections is a good library as well, but it has long failed to provide a generics-enabled version (which is a major drawback for a collections API in my opinion) and generally seems to be in a maintenance/don't-do-too-much-work-on-it mode Recently Commons Collections has picked up some steam again, but it has some catching up to do..

If download size/memory footprint/code size is an issue then Apache Commons Collections might be a better candidate, since it is a common dependency of other libraries. Therefore using it in your own code as well could potentially be done without adding any additional dependencies. Edit: This particular "advantage" has been partially subverted by now, since many new libraries actually depend on Guava and not on Apache Commons Collections.

Solution 2

From the faq: Google Collections FAQ

Why did Google build all this, when it could have tried to improve the Apache Commons Collections instead?

The Apache Commons Collections very clearly did not meet our needs. It does not use generics, which is a problem for us as we hate to get compilation warnings from our code. It has also been in a "holding pattern" for a long time. We could see that it would require a pretty major investment from us to fix it up until we were happy to use it, and in the meantime, our own library was already growing organically.

An important difference between the Apache library and ours is that our collections very faithfully adhere to the contracts specified by the JDK interfaces they implement. If you review the Apache documentation, you'll find countless examples of violations. They deserve credit for pointing these out so clearly, but still, deviating from standard collection behavior is risky! You must be careful what you do with such a collection; bugs are always just waiting to happen.

Our collections are fully generified and never violate their contracts (with isolated exceptions, where JDK implementations have set a strong precedent for acceptable violations). This means you can pass one of our collections to any method that expects a Collection and feel pretty confident that things will work exactly as they should.

Solution 3

The most important things I've found that make Google Collections the place to start:

  • Generics (Collections without Generics -- FTL)
  • Consistency with Collections framework (Josh Bloch was a key player in this framework)
  • Correctness. These guys are desperately tied to getting this problem right; they have something like 25K unit tests, and are tied to getting the API just right.

Here's a great Youtube video of a talk that was given by the primary author and he does a good job of discussing what is worth knowing about this library.

Share:
68,444

Related videos on Youtube

Joonas Pulakka
Author by

Joonas Pulakka

I'm the glue that holds the gears of progress together.

Updated on September 09, 2020

Comments

  • Joonas Pulakka
    Joonas Pulakka over 3 years

    I was looking for a bidirectional map implementation in Java, and stumbled upon these two libraries:

    Both are free, have the bidirectional map implementation that I was looking for (BidiMap in Apache, BiMap in Google), are amazingly nearly the same size (Apache 493 kB, Google 499 kB) [ed.: no longer true!] and seem in all ways pretty similar to me.

    Which one should I choose, and why? Are there some other equivalent alternatives (must be free and have at least the bidirectional map)? I'm working with the latest Java SE, so no need to artificially limit to Java 5 or anything like that.

    • SteveD
      SteveD over 14 years
      Surely you should give us the criteria for choosing the library? License, performance, additional dependencies, supporting generics, ...
    • Joachim Sauer
      Joachim Sauer over 14 years
      Google Collections is available on repo1.maven.org: repo1.maven.org/maven2/com/google/collections/…
    • kdgregory
      kdgregory over 14 years
      I stand corrected -- I was looking in com/googlecode
  • Ravi Wallau
    Ravi Wallau over 14 years
    I haven't use the Google Collections for a few months now. I started using it because of the Multimap interface and its implementations, and now I use the static methods in the classes Sets, Lists and Maps for everything that I can. I haven't used MapMaker yet, but I sure will look at it more closely - I have special interest in the weak values implementation.
  • Joachim Sauer
    Joachim Sauer over 14 years
    What I really wonder: why are there no other opinions? Should I be playing the devils advocate? Apache Commons Collections is not a bad library, after all.
  • Joonas Pulakka
    Joonas Pulakka over 14 years
    Since the Apache lacks generics, I think it's obvious which of these two is the future. Google is the next logical step forward. It's a strange feeling, though, that it's built by The Giant... but as long as it's under a free license it shouldn't matter even if it was built by Microsoft. I guess.
  • Jonik
    Jonik over 14 years
    Great further reading about Google Collections: javalobby.org/articles/google-collections (an interview with its main creators). Look for the question "What is unique about your approach? How does it differ to, for example, the Apache Commons Collection?"
  • Xaerxess
    Xaerxess about 12 years
    Sources? You mean jar which can be attached in Eclipse? It's here. BTW: What's wrong with git clone https://code.google.com/p/guava-libraries/ and git checkout v11.0.2?
  • Xaerxess
    Xaerxess over 11 years
    If you want to use Multimap like Map, there's always asMap() view.
  • sleske
    sleske over 11 years
    How do you expect a Multimap to implement java.util.Map? A multi-map is fundamentally different from a map.
  • matt
    matt over 11 years
    Multimap<K,V> extends Map<K,Collection<V>>.. I suspect G had good reason not to use Map as superinterface though.
  • ruakh
    ruakh over 11 years
    @lucek: If you look through the Javadoc for Map, with the understanding that every reference to V will actually be a Collection<V>, I think you'll see pretty quickly why it's not a good superinterface for Multimap<K, V>.
  • Abdull
    Abdull almost 11 years
    Apache Commons Collections Version 4 uses generics. commons.apache.org/proper/commons-collections/release_4_0.ht‌​ml
  • Roy Truelove
    Roy Truelove over 9 years
    Readers should be aware that this is a very old answer and much has changed
  • joeA
    joeA over 9 years
    @RoyTruelove It doesn't surprise me that much has changed and I would love to hear your current thoughts on the matter, or perhaps a link to a more recent review/comparison. I like the "immutable by default / mutable only if required" philosophy and the inclusion of generics in guava, but the materials I have been reading may all have been dated like you say this thread has become.
  • MasterJoe
    MasterJoe over 7 years
    @RoyTruelove - Can you please tell us what has changed & whether we can continue to use the guava/google collections libraries ? They have not changed since 2010. I wonder if this is because the code is complete and there are no bugs, or if the project was abandoned.
  • Roy Truelove
    Roy Truelove over 7 years
    @testerjoe2 - Sorry, I wrote that comment a long time back and frankly don't remember the reason for it. In hindsight it was a pretty unhelpful one! I didn't realize that the libs haven't changed since 2010, but I do know that they continue to be heavily used, and so I'd say should be safe. If I were starting w/ a new project today I'd probably have a close look at Goldman Sach's collection lib: github.com/goldmansachs/gs-collections. When you're one of the world's most evil companies you really should make sure you've got a kickass java collections library.
  • MasterJoe
    MasterJoe over 7 years
    @RoyTruelove - Thanks. Is the GS collections as frequently used or cited in the software dev industry ?
  • Roy Truelove
    Roy Truelove over 7 years
    @testerjoe2 probably not. I generally use Apache if I have no specific criteria that I need to meet.
  • vancan1ty
    vancan1ty over 7 years
    @testerjoe2's comment that the libraries haven't changed much since 2010 is incorrect. Commons Collections, for instance, had a major v4.0 release near the end of 2013 and now supports generics. It would be interesting to see a comparison of the two libraries as they stand today.