[英]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 我想在Java中压缩任意深度嵌套的某些类型T元素的集合/结构 ,最好用
Is there a java library which can handle this? 有没有一个可以处理这个的java库?
Guava seems to only handle one nesting level, ie Collection<Collection<T>>
--flatten--> Collection<T>
. 番石榴似乎只处理一个嵌套级别,即
Collection<Collection<T>>
--flatten - > Collection<T>
。
lambdaj looks promising: can I somehow combine on()
, asIterator()
and flattenIterator()
to achieve this? lambdaj看起来很有希望:我可以以某种方式结合
on()
, asIterator()
和flattenIterator()
来实现这一目标吗? In a statically type-safe manner? 以静态类型安全的方式?
Guava will probably support this eventually : 番石榴最终可能会支持这个:
http://code.google.com/p/guava-libraries/issues/detail?id=174 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.) (这可能是最简单的从下到上阅读,因为它的思考在其生命周期中已经转移了几次。)
不是基于上述任何一种,而是使用Collection构造的DeepIterator类,其next()方法查看下一个Object,如果它是一个instanceof,则将当前迭代器推送到Stack并递归到该Collection的迭代器中。
我认为这可能会有所帮助: Arrays.deepToString(myCollection.toArray())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.