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

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

    FYI, there's a reasonable way to do this in Guava without Java 8:

    ImmutableSortedSet set = ContiguousSet.create(
        Range.closedOpen(0, 7), DiscreteDomain.integers());
    ImmutableList list = set.asList();
    

    If you don't actually need the List semantics and can just use a NavigableSet, that's even better since a ContiguousSet doesn't have to actually store all the elements in it (just the Range and DiscreteDomain).

提交回复
热议问题