首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将Java8流收集到Guava ImmutableCollection中?

如何将Java8流收集到Guava ImmutableCollection中?
EN

Stack Overflow用户
提问于 2015-03-12 23:01:52
回答 5查看 24.8K关注 0票数 83

我想做以下几件事:

List<Integer> list = IntStream.range(0, 7).collect(Collectors.toList());

但在某种程度上,结果列表是Guava的ImmutableList的实现。

我知道我能做到

List<Integer> list = IntStream.range(0, 7).collect(Collectors.toList());
List<Integer> immutableList = ImmutableList.copyOf(list);

但我想直接收集到它。我试过了

List<Integer> list = IntStream.range(0, 7)
    .collect(Collectors.toCollection(ImmutableList::of));

但它抛出了一个异常:

com.google.common.collect.ImmutableCollection.add(ImmutableCollection.java:96)上的

java.lang.UnsupportedOperationException

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29013250

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档