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

后端 未结 5 1380
你的背包
你的背包 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-13 23:53

    BTW: since JDK 10 it can be done in pure Java:

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

    Also toUnmodifiableSet and toUnmodifiableMap available.

    Inside collector it was done via List.of(list.toArray())

提交回复
热议问题