作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想执行以下操作:
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));
但它抛出了异常:
java.lang.UnsupportedOperationException at com.google.common.collect.ImmutableCollection.add(ImmutableCollection.java:96)
最佳答案
Alexis 接受的答案中的 toImmutableList()
方法现在包含在 Guava 21 中并可用作:
ImmutableList<Integer> list = IntStream.range(0, 7)
.boxed()
.collect(ImmutableList.toImmutableList());
编辑:从 ImmutableList.toImmutableList
中删除了 @Beta
以及 Release 27.1 中的其他常用 API (6242bdd)。
关于java-8 - 如何将 Java 8 流收集到 Guava ImmutableCollection 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29013250/
我是一名优秀的程序员,十分优秀!