首页 文章

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

提问于
浏览
10

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

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 回答

  • 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 获取一个迭代器;然后在 mapToObj 中调用 next() .

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

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

    最简单的方法是流索引:

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

相关问题