鉴于一堆具有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 StreamallThings(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);