给定一堆具有 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??
}
我的尝试使用了老式的Iterable
并流式传输了看起来不必要的困惑。
最佳答案
也许是这样的:
return IntStream.range(0, things.size())
.mapToObj(things::get)
.map(Thing::new);
关于java - 将多个索引项转换为流的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34063652/