本文介绍了用于Guava不可变集合的Java 8收集器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常喜欢Java 8流和Guava的不可变集合,但我无法弄清楚如何将两者结合使用。

I really like Java 8 streams and Guava's immutable collections, but I can't figure out how to use the two together.

例如,我该如何实现收集的Java 8 将结果流转换为?

For example, how do I implement a Java 8 Collector that gathers stream results into an ImmutableMultimap?

奖励积分:我希望能够提供键/值映射器,类似于有效。

Bonus points: I'd like to be able to provide key/value mappers, similar to how Collectors.toMap() works.

推荐答案

从版本21开始,你可以

Since version 21, you can

.collect(ImmutableSet.toImmutableSet())
.collect(Maps.toImmutableEnumMap())
.collect(Sets.toImmutableEnumSet())
.collect(Tables.toTable())
.collect(ImmutableList.toImmutableList())
.collect(Multimaps.toMultimap(...))

这篇关于用于Guava不可变集合的Java 8收集器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 01:47