0

Possible Duplicate:
Rule of thumb for choosing an implementation of a Java Collection?

My situation is this:

  • I have a collection of objects that I need to hold and every now and then iterate through
  • The size of the collection is dynamic
  • The iteration should access each element
  • The collection does not need to be sorted

Creating or updating the collection has no time constraints but I'd like to iterate through the collection as fast as possible.

What would be the best Collection to use (or would you perhaps suggest using an array?)

Community
  • 1
  • 1
Lieuwe
  • 1,734
  • 2
  • 27
  • 41

2 Answers2

2

You can use a List collection probaly ArrayList.

It depends on the parameters like whether it is an ordered collection, whether you want to maintain the insertion order, whether you want to maintain uniqueness etc

List vs Set
Set: Unique, unordered collection
List: ordered collection, allows duplicate elements

ArrayList vs LinkedList

Community
  • 1
  • 1
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
2

In general : If you don't have particular constraints, an ArrayList is you best bet. Unless you have extremely tight performance control, don't go for a blank array, you have too much chance for errors (and a big chance of not outperforming the ArrayList implementation).

In your case the fast iteration requirements means ArrayList is a good choice.

Thirler
  • 20,239
  • 14
  • 63
  • 92