views:

77

answers:

3

Is there a standard Java (1.5+) implementation (i.e. no 3rd party) of a collection which allows me to glue several collections into one?

Here's a sketch, of how it works:

final SomeCollection x = new SomeCollection();
final ArrayList a = new ArrayList();

a.add("first");
assert(a.size() == 1);

x.embed(a); // don't know for sure, if this exists.
assert(x.size() == 1);

a.add("second");
assert(a.size() == 2);
assert(x.size() == 2); // the other array is used as a backend-collection.

final ArrayList b = new ArrayList();
b.add("third");
assert(b.size() == 1);

x.embed(b);
assert(x.size() == 3); // x is a combination of the other two lists.

Thanks!

+2  A: 

There isn't a standard collection that nests collections with the semantics you're after.

cletus
+1  A: 

One, I'm not sure if X is a collection of ArrayList objects or if X is a collection that changes following the elements added to A. You should add a bit of type safety (and self documentation) to your code, i.e.

    final List<String> x = new ArrayList<String>();    
    final List<String> a = new ArrayList<String>();

    a.add("first");    
    assert(a.size() == 1);  

Two, collections (with an s) is full of collections "backed" with another, e.g.

    Collections.unmodifiableCollection(Collection<? extends T> c)
kiwicptn
A: 

If the 'SomeCollection' class mentioned by you is of type java.util.Collection, then you can add any other collection to it. See for eg. you can combine two different type of collection as follows.

Vector  v = new java.util.Vector();
ArrayList alist = new java.util.ArrayList();
v.addAll(alist);
lucentmind
It will not reflect changes from the ArrayList back into the Vector, which was the feature I required.I've changed the design of the code; I don't need this type of Collection anymore.It would be nice if there will be a Java implementation for such a Collection soon, I can imagine more people than me needing it.
Pindatjuh