本文介绍了Google Guava 与 Apache Commons的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找 Java 中的 双向映射 实现,并偶然发现了这两个库:

两者都是免费的,具有我正在寻找的双向地图实现(Apache 中的 BidiMap,Google 中的 BiMap),大小几乎相同(Apache 493 kB,Google 499 kB) 并且在所有方面都与我非常相似.

我应该选择哪一个,为什么?是否有其他等效的替代方案(必须是免费的并且至少有双向地图)?我正在使用最新的 Java SE,因此无需人为地限制为 Java 5 或类似的东西.

解决方案

在我看来,更好的选择是 Guava(以前称为 Google 集合):

  • 它更现代(有泛型)
  • 它绝对符合 Collections API 要求
  • 积极维护
  • CacheBuilder 及其前身 MapMaker 简直太棒了

Apache Commons Collections 也是一个很好的库,但它长期以来一直未能提供支持泛型的版本(在我看来,这是集合 API 的一个主要缺点)并且通常似乎处于维护/不要做太多工作的模式 .>

如果下载大小/内存占用/代码大小是一个问题,那么 Apache Commons Collections 可能是更好的候选者,因为它是其他库的常见依赖项.因此,在您自己的代码中使用它也可能无需添加任何额外的依赖项.这个特殊的优势"现在已经部分被颠覆,因为许多新库实际上依赖于 Guava 而依赖于 Apache Commons Collections.

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.

解决方案

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 .

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.

这篇关于Google Guava 与 Apache Commons的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 15:38