Possible Duplicate:
Combine multiple Collections into a single logical Collection?
I have a lot of small collections that I would like to access as a whole.
I could e.g. create a new LinkedList and add all elements to it. But I was thinking, there might be some tool to add complete Collections without having to re-link every item. I am not speaking about addAll. The Collections shall stay seperate, but only from the outside seem to be one.
Any ideas?
The Google Guava project has a class called
Iteratorsthat provides utilities for taking iterators and applying transforms to them. One of the functions they provide is calledIterators.concat, which takes in multiple iterators and produces a new iterator that iterates over the concatenation of those individual iterators. This may not be exactly what you’re looking for, but it would provide you the functionality of iterating across multiple elements without having to duplicate all of them.One question you may need to think about when using a collection as you’ve proposed is what happens if you try to mutate the collection. For example, if you add an element, where would it go? If all you need is iteration and you don’t need to think about this, the
Iterators.concatmethod may be what you’re looking for.