Re: Stream based API for tree like structures
Hi Kasper, If what you're looking for is a more immediate solution, I can think of a couple of libraries which may do the trick: 1. Durian <https://github.com/diffplug/durian>'s TreeDef and TreeStream. Difficult to understand and use at first, but I believe it ticks a good number of boxes for you. 2. Guava 21 <https://github.com/google/guava/wiki/Release21>'s `TreeTraverser` with the library's new Java 8 support. For example, `Streams.stream(TreeTraverser.using(Node::getChildNodes).{preOrder|postOrder|breadthFirst}Traversal(rootNode));` Both of these solutions allow depth- and breadth-first traversals (the Guava solution allows both preOrder and postOrder depth traversals), but it's not clear to me if either of them allows you to set a maximum depth to traverse into. Hope this helps. Best regards, Jonathan On 20 January 2017 at 15:00, Remi Forax wrote: > https://gist.github.com/forax/bca6877e019d134f87c4cb1e8efae9cd > > Rémi > > - Mail original - > > De: "Kasper Nielsen" > > À: "core-libs-dev" > > Envoyé: Vendredi 20 Janvier 2017 11:03:41 > > Objet: Stream based API for tree like structures > > > 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 stream(TreeStreamOptions... options) > > Stream parallelStream(TreeStreamOptions... options) > > > > another one could be > > > > class TreeStreamOptions { > > TreeStreamOptions setDepthFirst(); > > TreeStreamOptions setBreathFirst(); > > TreeStreamOptions setDepth(); (0 sieblings only, Integer.MAX->infinity) > > } > > Stream stream(TreeStreamOptions options) > > Stream 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 >
Re: Stream based API for tree like structures
https://gist.github.com/forax/bca6877e019d134f87c4cb1e8efae9cd Rémi - Mail original - > De: "Kasper Nielsen" > À: "core-libs-dev" > Envoyé: Vendredi 20 Janvier 2017 11:03:41 > Objet: Stream based API for tree like structures > 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 stream(TreeStreamOptions... options) > Stream parallelStream(TreeStreamOptions... options) > > another one could be > > class TreeStreamOptions { > TreeStreamOptions setDepthFirst(); > TreeStreamOptions setBreathFirst(); > TreeStreamOptions setDepth(); (0 sieblings only, Integer.MAX->infinity) > } > Stream stream(TreeStreamOptions options) > Stream 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
Stream based API for tree like structures
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 stream(TreeStreamOptions... options) Stream parallelStream(TreeStreamOptions... options) another one could be class TreeStreamOptions { TreeStreamOptions setDepthFirst(); TreeStreamOptions setBreathFirst(); TreeStreamOptions setDepth(); (0 sieblings only, Integer.MAX->infinity) } Stream stream(TreeStreamOptions options) Stream 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