我使用Guava的不可变集合(为Java 6编译)和一个Java 8 JRE,我可以使用新的流接口吗? [英] I use Guava's immutable collections (compiled for Java 6) and a Java 8 JRE, can I use the new stream interface?

查看:189
本文介绍了我使用Guava的不可变集合(为Java 6编译)和一个Java 8 JRE,我可以使用新的流接口吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Guava,特别是他们的不可变集合( ImmutableList ImmutableSet )。

I use Guava and in particular their immutable collections (ImmutableList, ImmutableSet).

但是Guava是为Java 6编译的。如果我使用Java 8,我可以使用 .stream()他们?

But Guava is compiled for Java 6. If I use Java 8, can I use .stream() with them?

推荐答案

是的。

$ c> .stream()方法,它在 Collection 界面中定义,有默认实现。因此, .parallelStream() .spliterator() List s和 Set s都是 Collection

The .stream() method, which is defined in the Collection interface, has a default implementation. And so do, for that matter, .parallelStream() and .spliterator(). And both Lists and Sets "are" Collections.

它不会结束,因为你也可以使用 映射 .forEach code> 在Guava的 ImmutableMap 上。 Map 有其他默认操作,但它们改变了地图,Guava的不可变集合/地图是...嗯...

And it doesn't end there since you can also use Map's .forEach() on Guava's ImmutableMaps as well. Map does have other default operations, but they mutate the map, and Guava's immutable collections/maps are... Well...

请注意,一般来说,每当一个接口的方法有一个默认实现时,它将在javadoc中被提及,因为方法的返回类型将以 default 关键字

Note that more generally, each time an interface's method has a default implementation, it will be mentioned in the javadoc, since the method's return type will be preceded with the default keyword.

一些示例,非常粗略的代码工作和使用lambdas中使用的单抽象方法的概念,请参见此处):

Some sample, very crude code which works and makes use of that (along with the concept of Single Abstract Method used in lambdas, see here for more details):

ImmutableSet.of(23, 2389, 19).stream().forEach(System.out::println);

System.out c $ c> PrintStream ,及其 .println()方法签名与 Consumer

(System.out is a PrintStream, and its .println() method signature is the same as that of a Consumer)

这篇关于我使用Guava的不可变集合(为Java 6编译)和一个Java 8 JRE,我可以使用新的流接口吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆