Google guava和Apache commons哪个好?

发现它们两个工具包中有重叠的部分,我在项目中要优先使用谁呢?或者说两者都使用呢?但是混用肯定是不太好的,求指点?
关注者
138
被浏览
142,345

9 个回答

Guava 的 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.


简单地说:

  1. Apache Commons Collections 3.x 不支持泛型,Guava 支持
  2. Guava 实现了 JDK 的标准接口,而 Apache Commons Collections 3.x 有很多违反标准的地方

感谢

@小猪

提醒,补充 Apache Commons Collections 4.x 的发行注记如下:

Major changes since 3.2.1
  • Use of generics and other language features introduced in Java 5 (varargs, Iterable)
  • Removed deprecated classes / methods and features which are now supported by the JDK
  • Replaced Buffer interface with java.util.Queue
  • Added concept of split maps with respective interfaces Put / Get (see also package splitmap)
  • Added new Trie interface together with an implementation of a Patricia Trie

从 4.x 开始,Apache Commons Collections 开始使用 JDK 5 的特性(包括泛型),此外也去除、添加了很多内容,详细请参考

这里

建议:

要学会搜索,“XXX 和 YYY 那个更好” 这类问题,你自己 Google 就有答案,例如

"Apache Commons Collections vs. Guava"

"Apache Commons Collections Guava which is better ?"

学技术,最好先实践对比,时间紧的话,请善用搜索引擎。

P.S

Java 学习路线,请参考:

会计转行从事IT,如何在一年时间内全职学习? - Night Silent 的回答

混用为什么不好?我就混用啊。。。