Skip to content

Instantly share code, notes, and snippets.

@dr0i
Created October 12, 2018 13:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dr0i/a66ba0014057a5e3c4b863f70f4d6212 to your computer and use it in GitHub Desktop.
Save dr0i/a66ba0014057a5e3c4b863f70f4d6212 to your computer and use it in GitHub Desktop.
public class RecursionStream {
public static void main(String... args) {
getAllJsonNodes(node)//
.peek(p -> System.out.println("endpeek" + p))
.forEach(p -> System.out.println("end" + cnt + ":" + p.toString()));
}
private Stream<JsonNode> getAllJsonNodes(JsonNode node) {
cnt++;
Iterable<Entry<String, JsonNode>> iterable = () -> node.fields();
return StreamSupport.stream(iterable.spliterator(), false)
.filter(e -> e.getValue().getNodeType().equals(JsonNodeType.OBJECT))//
.peek(p -> System.out.println("cnt" + cnt + ":" + p))//
.flatMap(m -> getAllJsonNodes(m.getValue()));
}
}
@dr0i
Copy link
Author

dr0i commented Oct 12, 2018

"endpeek" and "end" is never printed, why is this?

@dr0i
Copy link
Author

dr0i commented Oct 12, 2018

Though without the terminalOperation "forEach" it wouldn't start to begin with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment