Create Java 8 Stream from ArrayNode

icl7126 picture icl7126 · Sep 20, 2015 · Viewed 15.4k times · Source

Is it possible to create stream from com.fasterxml.jackson.databind.node.ArrayNode?
I tried:

ArrayNode files = (ArrayNode) json.get("files");
Stream<JsonNode> stream = Stream.of(files);

But it will actually give stream of one element, the initial ArrayNode object.
Correct result should be Stream<JsonNode>, can I achieve it?

Answer

JB Nizet picture JB Nizet · Sep 20, 2015

ArrayNode implements Iterable. Iterable has a spliterator() method. You can create a sequential Stream from a Spliterator using

StreamSupport.stream(spliterator, false)