views:

146

answers:

5

I want to create a collection of objects that implement a given interface (in C# it would be something like Collection<IMyInterface>). I don't care about sorting or indexed access but would like to easily iterate over the collection with foreach (or the Java equivalent). It also needs to have as small a memory footprint as possible (and ideally be threadsafe).

What do you recommend?

Thanks

+5  A: 

You should read Java Collections tutorial and choose what fits your needs most.

Dmitry
+3  A: 

Check out the Java Collection Tutorial. That will introduce you to the Java collection idioms, the generics support, and what's available (briefly, Lists, Sets and Maps - it's not clear how you want to store this data).

Without any further info, and in the most trivial scenario, you may want something like:

List<IMyInterface> list = new ArrayList<IMyInterface>();
// populate and then iterate...
list.add(new MyObject());
for (IMyInterface o : list) {
   // do something...
}

(ignoring thread-safety issues)

Once you're au fait with the standard collections, it's worth looking at more advanced options like Google Collections and Apache Commons Collections. These both offer more powerful collections and tools for iteration/manipulation.

Note (finally) that Vector and the like are advertised as being thread-safe. That (of course) only applies per-method-call. A collection inspection/addition operation will not be thread-safe natively and will require some extra synchronisation surround multiple operations.

Brian Agnew
A: 

Java Collections Framework contains many of the classes you may find useful. They have both threadsafe and non-threadsafe candidates.

fastcodejava
It's worth noting that the above link is to a very old version of the Java API. It's so old that it precedes Generics, which should be used for any new Java development.
Jason Nichols
This is the correct link: http://java.sun.com/javase/6/docs/technotes/guides/collections/index.html
R. Bemrose
Link modified, +1 for the correction.
fastcodejava
+2  A: 

Your best bet it to do one of these:

import java.util.*;
import java.util.concurrent.*;

// ...

// Thread-safe, but creates a new copy on write... so writers can write new
// values that readers won't see
List<IMyInterface> = new CopyOnWriteArrayList<IMyInterface>();

or

import java.util.*;

// ...

// also thread-safe, collection is locked during writes
List<IMyInterface> = Collections.synchronizedList(new ArrayList<IMyInterface>());

Edit: Another answer points out that the Vector class also implements the List interface and is thread-safe, so you could use that instead of the second option here.

R. Bemrose
You most certainly do not want to use CopyOnWriteArrayList for anything but rather specialized uses - compared to the normal collections it will be obscenely slow for write-heavy loads...
Steven Schlansker
+2  A: 

You might want to use Vector which is thread-safe and will do your purpose in this case.

Collection<Integer> collection = new Vector<Integer>();

collection.add(1); //add something to the collection

//iterate over the collection
for(Integer test : collection) {
  //do something with 
}
Jobaer
Vector's are considered obsolete by many, see http://stackoverflow.com/questions/1386275/why-java-vector-class-is-considered-obsolete-or-deprecated
Pool