Stream based API for tree like structures

Kasper Nielsen kasperni at gmail.com
Fri Jan 20 10:03:41 UTC 2017


Hi,

Sorry if this is a bit off-topic, but I thought but I thought it might have
some general interest if Java ever got some proper tree/graph collection
classes.

Has anyone developed a stream based API that allows for tree based travels.
I'm mainly thinking about functionality for

1)
Whether or not to do recursive traversal of all child nodes, only 1 level
of child nodes, or just siblings

2)
Order of traversal: depth/breadth first.

I'm trying to avoid an explosion of methods such as
streamSieblingsDepthOrderFirst.
One thought I had was taking a var arg of options to stream and
parallelStream such as:
enum TreeStreamOptions {
   SIEBLINGS_ONLY, RECURSIVELY, DEPTH_FIRST, BREATH_FIRST;
}
Stream<T> stream(TreeStreamOptions... options)
Stream<T> parallelStream(TreeStreamOptions... options)

another one could be

class TreeStreamOptions {
   TreeStreamOptions setDepthFirst();
   TreeStreamOptions setBreathFirst();
   TreeStreamOptions setDepth(); (0 sieblings only, Integer.MAX->infinity)
}
Stream<T> stream(TreeStreamOptions options)
Stream<T> parallelStream(TreeStreamOptions options)

While a visitor pattern would make sense for many use cases. I really like
the simplicity of just working with streams.

Maybe someone has some thoughts on this.

Best
  Kasper


More information about the core-libs-dev mailing list