将大量索引项转换为流的最简单方法

Old*_*eon 2 java java-8 java-stream

鉴于一堆具有int size()方法和get(int i)方法的东西,它如何最容易流式传输?

import nu.xom.Builder;
import nu.xom.Element;
import nu.xom.Elements;

// My builder.
Builder builder = new Builder();

class Thing {
    public Thing(Element from) {
        // Construct from an Element.
    }
}

private Stream<Thing> allThings(Path path) throws FileNotFoundException, ParsingException, IOException {
    Elements things = builder.build(new FileInputStream(path.toFile()))
            .getRootElement().getChildElements();
    // Return a stream of `Thing`s created from all of the children.
    // How??
}
Run Code Online (Sandbox Code Playgroud)

我的尝试使用了一所旧式学校,Iterable并且流式传输似乎不必要的杂乱.

ass*_*ias 5

也许是这样的:

return IntStream.range(0, things.size())
          .mapToObj(things::get)
          .map(Thing::new);
Run Code Online (Sandbox Code Playgroud)