且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java 8列表与流映射

更新时间:2023-10-05 15:32:52

您可以使用 IntStream 创建 Stream 的索引,然后将它们转换为a Map

You can create a Stream of the indices using an IntStream and then convert them to a Map :

Map<Integer,Item> map = 
    IntStream.range(0,items.size())
             .boxed()
             .collect(Collectors.toMap (i -> i, i -> items.get(i)));