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

后端 未结 5 1385
你的背包
你的背包 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:06

    The toImmutableList() method in the accepted answer of Alexis is now included in Guava 21 and can be used as:

    ImmutableList list = IntStream.range(0, 7)
        .boxed()
        .collect(ImmutableList.toImmutableList());
    

    Edit: Removed @Beta from ImmutableList.toImmutableList along with other frequently used APIs in Release 27.1 (6242bdd).

提交回复
热议问题