有一种简洁的方法可以在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
提交
取消