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

时间:2015-03-11 14:51:09

标签: 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();
}

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

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

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

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
            ));
}

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

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

2 个答案:

答案 0 :(得分: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));
}

然而,根据列表的底层实现,它可能效率不高。你可以从IntStream获取一个迭代器;然后在next()。{/ p>中调用mapToObj

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

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

答案 1 :(得分:8)

最简单的方法是传输索引:

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