220

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.

3
  • 5
    Surely you should give us the criteria for choosing the library? License, performance, additional dependencies, supporting generics, ...
    – SteveD
    Sep 18, 2009 at 13:13
  • 1
    Google Collections is available on repo1.maven.org: repo1.maven.org/maven2/com/google/collections/… Sep 18, 2009 at 13:21
  • I stand corrected -- I was looking in com/googlecode
    – kdgregory
    Sep 18, 2009 at 14:22

5 Answers 5

190

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.

10
  • 3
    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. Sep 20, 2009 at 9:53
  • 10
    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. Sep 21, 2009 at 10:26
  • 15
    Readers should be aware that this is a very old answer and much has changed Nov 21, 2014 at 22:29
  • 1
    @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.
    – joeA
    Nov 22, 2014 at 22:01
  • 2
    @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. Sep 8, 2016 at 2:06
79

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.

73

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.

2
-7

Two other things (I hope I'm not wrong)

  • The licence of Guava (new name for google collections) is the Apache License 2.0, meaning: the same one as for Apache Commons project
  • I cannot find the source code of Guava in a file to download (it seems only a git-access is possible)
1
  • 20
    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? Apr 5, 2012 at 13:36
-7

One nasty thing about Guava is that Multimap does not extend java.util.Map. If You have Your own methods that work on Maps they won't work on Guava Multimaps (Apache MultiMap interface does extend java.util.Map). I'm sure that there's some good reason why it's the way it's but it's also inconvenient.

4
  • 16
    If you want to use Multimap like Map, there's always asMap() view. Sep 3, 2012 at 8:55
  • 23
    How do you expect a Multimap to implement java.util.Map? A multi-map is fundamentally different from a map.
    – sleske
    Oct 16, 2012 at 14:01
  • 1
    Multimap<K,V> extends Map<K,Collection<V>>.. I suspect G had good reason not to use Map as superinterface though.
    – matt
    Oct 17, 2012 at 9:51
  • 10
    @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>.
    – ruakh
    Nov 6, 2012 at 18:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.