Google guava 与 Scala 集合框架比较
有很多常见的概念:
- 不可变集合、
- 集合视图、
- 严格/非严格集合、
- 集合构建器
以及 Guava 和 Scala Collection API 中的相同模式。 那么有什么区别呢?两个库是否符合模式?扩展的简易性是否足够好?
所以我想听听同时使用这两个框架的人对它们的比较。
There are a lot of common concepts:
- immutable collection,
- collection view,
- strict/non strict collection,
- collection builders
same patterns in Guava and Scala Collection API.
So what is the difference? Is both library are consistent with patterns? Is easablity of extension is good enought?
So I would like to hear comparison of those frameworks from someone who use them both.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Google Guava 是一个非常棒的库,这是毫无疑问的。然而,它是用 Java 实现的,并受到所有限制,这意味着:
Guava 还必须存在于 Java 标准集合库的存在下,因此第 3 方库很少会公开与 guava 兼容的函数文字或利用番石榴特定的集合类型。这会导致您使用的每个第三方库的阻抗不匹配。例如,您通常希望将从此类库返回的集合转换为适当的 guava 不可变集合 - 特别是在多线程环境中工作时。
Scala 集合的设计能够更好地集成到语言中,您会发现它们在整个 scala 标准库以及在 Scala 中实现的第三方产品中得到广泛使用。默认情况下,Scala 集合也是不可变的,因此您最终会得到更安全的代码,不需要额外的防御包装层。
如果您可以使用 Scala,那么就使用它,它除了集合框架之外还有很多优点。如果您必须使用 Java,那么 Guava 是一个不错的选择,特别是考虑到如果没有 Scala 提供的语言功能,Scala 集合就不是特别容易使用。
在混合项目中,Guava 集合可以在 Scala 中完美使用,但该语言还提供了允许您使用 Java 集合(包括 Guava 集合,它们公开相同接口)的机制,就像它们是 Scala 自己的集合一样。
Google Guava is a fantastic library, there's no doubt about it. However, it's implemented in Java and suffers from all the restrictions that that implies:
Guava also has to exist in the presence of Java's standard collection library, so it's rare that 3rd party libraries will expose guava-compatible function literals or make use of guava-specific collection types. This causes an impedance mismatch for every third party library that you use. For example, you'll typically want to convert returned collections from such libraries to the appropriate guava immutable collection - especially if working in a multithreaded environment.
Scala collections have a design that is far better integrated into the language, you'll find them widely used throughout the scala standard library and through 3rd party products implemented in Scala. Scala collections are also immutable by default, so you end up with far safer code that doesn't require an extra layer of defensive wrapping.
If you can use Scala, then do so, it has many advantages beyond just the collections framework. If you have to use Java, then Guava is a good choice, especially given that Scala collections aren't particularly easy to use without the language features that Scala provides.
In a mixed project, Guava collections are prefectly usable from within Scala, but the language also provides mechanisms allowing you to use Java collections (including Guava's, which expose the same interfaces) as though they were Scala's own collections.
我现在在所有 java 项目中都使用番石榴。它为具有相似模式的 java 集合提供了很好的功能。
然而,在java中编写闭包意味着直接定义大量匿名类,这是冗长和无聊的。
Scala 集合在设计(由于特征而导致部分实现的级联)和功能方面仍然优越。只需实现一小组方法即可轻松创建自己的集合并获得 scala 集合的所有优点。
I use guava in all my java projects now. It gives a nice functional touch to java collections with similar patterns.
However, writing closures in java mean defining lots of anonymous class directly which is verbose and boring.
Scala collections are still superior in term of design (with the cascade of partial implementations due to traits) and functionalities. It is easy to create your own collection and gain all the advantages of scala collections by just implementing a small set of methods.
其他人已经回答了您的问题,但我认为您错过了一个有趣的替代方案,即 Functional Java 库。它忽略 Java Collection API,并提供外观和行为类似于简化的 Scala 集合的不可变集合。
The others already answered your question, but I think you missed an interesting alternative, the Functional Java Library. It ignores the Java Collection API, and provides immutable collections that look and behave like simplified Scala collections.
我使用过 Scala、Google Collections 和 F#。最近,我一直在使用 Google 集合迭代器,但忽视了 F# 序列表达式的强大功能。与迭代器/序列相比,Scala 似乎更喜欢非严格(惰性)列表。
在 F# 和 Google 集合中(请参阅 google 迭代器),您可以转换和过滤迭代器,创建一个很好的推送工作管道,该管道表示为对象流。并不是说你不能在 Scala 中做到这一点,只是不常见。 F# 有一个很酷的管道运算符,用于过滤迭代器(序列)。
例如,我希望 Scala 的 Yield 表达式生成一个像 Python(或 F# 序列块)一样的迭代器,而不是返回一个列表。
两者非常相似,Scala 在速度和语法方面具有巨大的优势,但在使用它们时我感觉(我的观点):
注意,Scala 2.8 似乎对其集合进行了一些重大更改(我使用了旧版本的 Scala)。
I have used Scala, Google collections and F#. Lately I have been using Google collections Iterators and remiss of the power of F# sequence expressions. It seems Scala prefers non-strict (lazy) lists over iterators/sequences.
In F# and Google collections (see google Iterators) you can transform and filter iterators creating a nice push pipeline of work that is represented as stream of objects. Its not that you can't do this in Scala its just not common. F# has a cool pipe operator for filtering iterators (sequences).
For example I would expect Scala's yield expression to generate an iterator like Python (or F# sequence blocks) instead it returns a list.
Both are very similar and Scala has huge advantages in terms of speed and syntax but when using them I feel (my opinion):
NOTE it appears Scala 2.8 has made some big changes in its collections (I used an older version of Scala).