开发者

Type-safe flattening of nested collections/structures in Java

I would like to flatten arbitrary deeply nested collections/structures of elements of some type T in Java, optimally with

  • only having a live view and not a copied collection;
  • not only handling Collections, but also Iterator, arrays of T of arbitrary dimension, Iterable, and all these structures arbitrarily mixed and nested;
  • statical type-safety.

开发者_JS百科Is there a java library which can handle this?


Guava seems to only handle one nesting level, i.e. Collection<Collection<T>> --flatten--> Collection<T>.

lambdaj looks promising: can I somehow combine on(), asIterator() and flattenIterator() to achieve this? In a statically type-safe manner?


Guava will probably support this eventually :

http://code.google.com/p/guava-libraries/issues/detail?id=174

(It might be easiest to read that from the bottom up, since the thinking on it has shifted a few times over its lifetime.)


Not based on either of the above, but perhaps a DeepIterator class that is constructed with a Collection whose next() method looks at the next Object and if it is a instanceof Collection then pushes the current iterator on Stack and recurses into that Collection's iterator.


I think this may help: Arrays.deepToString(myCollection.toArray())

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜