如何使用Java 8流将列表的元素映射到它们的索引?

Jar*_*lak 10 java lambda java-8 java-stream

有一个字符串列表,我需要构建一个有效对的列表(string, its position in the list).目前我有使用google集合的代码:

public Robots(List<String> names) {
    ImmutableList.Builder<Robot> builder = ImmutableList.builder();
    for (int i = 0; i < names.size(); i++) {
        builder.add(new Robot(i, names.get(i)));
    }
    this.list = builder.build();
}
Run Code Online (Sandbox Code Playgroud)

我想使用Java 8流做到这一点.如果没有索引,我可以这样做:

public Robots(List<String> names) {
    this.list = names.stream()
            .map(Robot::new) // no index here
            .collect(collectingAndThen(
                    Collectors.toList(),
                    Collections::unmodifiableList
            ));
}
Run Code Online (Sandbox Code Playgroud)

要获得索引,我必须做这样的事情:

public Robots(List<String> names) {
    AtomicInteger integer = new AtomicInteger(0);
    this.list = names.stream()
            .map(string -> new Robot(integer.getAndIncrement(), string))
            .collect(collectingAndThen(
                    Collectors.toList(),
                    Collections::unmodifiableList
            ));
}
Run Code Online (Sandbox Code Playgroud)

但是,文档说映射函数应该是无状态的,但AtomicInteger它实际上是它的状态.

有没有办法将顺序流的元素映射到它们在流中的位置?

Ale*_* C. 16

你可以这样做:

public Robots(List<String> names) {
    this.list = IntStream.range(0, names.size())
                         .mapToObj(i -> new Robot(i, names.get(i)))
                         .collect(collectingAndThen(toList(), Collections::unmodifiableList));
}
Run Code Online (Sandbox Code Playgroud)

但是,根据列表的底层实现,它可能效率不高.你可以从中获取一个迭代器IntStream; 然后打电话next()mapToObj.

作为替代方案,proton-pack库定义了zipWithIndex流的功能:

 this.list = StreamUtils.zipWithIndex(names.stream())
                        .map(i -> new Robot(i.getIndex(), i.getValue()))
                        .collect(collectingAndThen(toList(), Collections::unmodifiableList));
Run Code Online (Sandbox Code Playgroud)


ass*_*ias 8

最简单的方法是流索引:

List<Robot> robots = IntStream.range(0, names.size())
                              .mapToObj(i -> new Robot(i, names.get(i))
                              .collect(toList());
Run Code Online (Sandbox Code Playgroud)