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

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

 public Robots(List names) { ImmutableList.Builder 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 names) { this.list = names.stream() .map(Robot::new) // no index here .collect(collectingAndThen( Collectors.toList(), Collections::unmodifiableList )); } 

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

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

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

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

你可以这样做:

 public Robots(List 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库为流定义zipWithIndexfunction:

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

最简单的方法是流索引:

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