我在寻找Java中的双向映射实现,偶然发现了这两个库:
谷歌番石榴(前身为“谷歌系列”)
Apache Commons Collections
两者都是免费的,具有我正在寻找的双向映射实现(Apache中的BidiMap,谷歌中的BiMap),惊人地几乎相同的大小(Apache 493 kB,谷歌499 kB)[编辑:不再正确!]而且看起来在各个方面都和我很相似。
我应该选择哪一个,为什么?是否存在其他等效的替代方案(必须是免费的并且至少有双向映射)?我使用的是最新的Java SE,所以不需要人为地限制Java 5或类似的东西。
常见问题:
谷歌集合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.