Tag: javastream

我怎样才能收集一个Java 8stream到一个番石榴ImmutableCollection?

我想要做以下事情: List<Integer> list = IntStream.range(0, 7).collect(Collectors.toList()); 但是结果列表是番石榴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 com.google.common.collect.ImmutableCollection.add(ImmutableCollection.java:96)