简体   繁体   English

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

[英]How to map elements of the list to their indices using Java 8 streams?

Having a list of strings, I need to construct a list of objects which are effectively pairs (string, its position in the list) . 有了一个字符串列表,我需要构建一个有效对(string, its position in the list) Currently I have such code using google collections: 目前我有使用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();
}

I would like to do this using Java 8 streams. 我想用Java 8流做到这一点。 If there was no index, I could just do: 如果没有索引,我可以这样做:

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

To get the index, I would have to do something like this: 要获得索引,我必须做这样的事情:

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

However, the documentation says that mapping function should be stateless, but the AtomicInteger is effectively its state. 但是,文档说映射函数应该是无状态的,但AtomicInteger实际上是它的状态。

Is there a way to map elements of the sequential stream to their positions in the stream? 有没有办法将顺序流的元素映射到它们在流中的位置?

You could do something like this: 你可以这样做:

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

However it may not be as efficient depending on the underlying implementation of the list. 但是,根据列表的底层实现,它可能效率不高。 You could grab an iterator from the IntStream ; 你可以从IntStream获取一个迭代器; then calling next() in the mapToObj . 然后在mapToObj调用next()

As an alternative, the proton-pack library defines the zipWithIndex functionality for streams: 作为替代方案, proton-pack库为流定义zipWithIndex功能:

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

The easiest way is to stream indices: 最简单的方法是流索引:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用java 8流和lambdas迭代和处理其值为元素列表的映射的值 - How to iterate and work on the values of a map whose values are a list of elements using java 8 streams and lambdas Map 索引集到对象集,使用 Java 流 - Map set of indices to set of Objects, using Java streams Java Map 与列表值使用流列出? - Java Map with List value to list using streams? 如何生成列表的 Map 的 Map(地图<string, map<enum, list<string> &gt;&gt;) 在 java 中使用流</string,> - How do I generate a Map of Map of List (Map<String, Map<Enum, List<String>>>) in java using streams 如何使用 Java 8 个流根据 map 参数返回的列表进行过滤 - How to filter based on list returned by map param using Java 8 streams 如何使用Java 8流映射到多个元素? - How to map to multiple elements with Java 8 streams? 如何使用 Java 流制作列表元素的所有变体? - How to make all variations of list elements using Java streams? 使用Java 8流,如何转换Map <String, Map<String, List<Person> &gt;&gt;到地图 <Integer, Map<String, Integer> &gt;? - Using Java 8 streams, how to transform Map<String, Map<String, List<Person>>> to Map<Integer, Map<String, Integer>>? Java 8 Streams - 使用流将相同类型的多个对象映射到列表 - Java 8 Streams - Map Multiple Object of same type to a list using streams 如何使用流将 map 个元素添加到它们的索引中? - How to map elements to their index using streams?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM