简体   繁体   English

Java中嵌套集合/结构的类型安全展平

[英]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元素的集合/结构 ,最好用

  • 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; 不仅处理集合,而且还有Iterator,任意维度的T数组,Iterable,以及任意混合和嵌套的所有这些结构;
  • statical type-safety. 静态类型安全。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM