有一種簡潔的方法可以在Java 8中使用索引來迭代流嗎?在訪問流中的索引時,是否有一種簡單的方法來迭代流?String[] names = {"Sam","Pamela", "Dave", "Pascal", "Erik"};List<String> nameList;Stream<Integer> indices = intRange(1, names.length).boxed();
nameList = zip(indices, stream(names), SimpleEntry::new)
.filter(e -> e.getValue().length() <= e.getKey())
.map(Entry::getValue)
.collect(toList());與LINQ示例相比,這似乎相當令人失望。string[] names = { "Sam", "Pamela", "Dave", "Pascal", "Erik" };var nameList = names.Where((c, index) => c.Length <= index + 1).ToList();有沒有更簡潔的方法?更重要的是,拉鏈似乎已經移動或被移除.
3 回答

慕碼人8056858
TA貢獻1803條經驗 獲得超6個贊
Streams.mapWithIndex()
Streams.mapWithIndex( Stream.of("a", "b", "c"), (str, index) -> str + ":" + index)) // will return Stream.of("a:0", "b:1", "c:2")
添加回答
舉報
0/150
提交
取消