How can I collect a Java 8 stream into a Guava ImmutableCollection?

后端 未结 5 1370
你的背包
你的背包 2020-12-13 23:48

I would like to do the following:

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

but in a way that the resu

5条回答
  •  醉梦人生
    2020-12-14 00:18

    While not a direct answer to my question (it does not use collectors), this is a fairly elegant approach which doesn't use intermediate collections:

    Stream stream = IntStream.range(0, 7).boxed();
    List list = ImmutableList.copyOf(stream.iterator());
    

    Source.

提交回复
热议问题