Reputation: 205
I want to store 50000 or more strings and I need to perform several operations like retrieval of a specific string, deletion of a specific string, etc. I have been given only two options to select from and these are array list and array to store them. From a performance point of view which one is better?
Upvotes: 1
Views: 1833
Reputation: 328608
Provided you intially size the ArrayList correctly, the main difference will come from additions, which do a range check that you could get rid of with an array. But we are talking about a few CPU cycles here.
Apart from that there should be no noticeable difference. For example, the indexOf
method in ArrayList looks like this:
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
Upvotes: 0
Reputation: 10891
If you look at the source code of ArrayList you will see:
107 /**
108 * The array buffer into which the elements of the ArrayList are stored.
109 * The capacity of the ArrayList is the length of this array buffer.
110 */
111 private transient Object[] elementData;
it is using an array internally.
So ArrayList could never be faster than using an array.
Upvotes: 0
Reputation: 236004
An array will always have better performance than an ArrayList
. In part, because when using an array you don't have to pay the extra cost of type-casting its elements (using generics doesn't mean that typecasts disappear, only that they're hidden from plain view).
To make my point: Trove and fastutil are a couple of very fast Java collections libraries, which rely on the fact of providing type-specific collections and not Object-based implementations like ArrayList
does.
Also, there's a cost for using a get()
method for accessing elements (albeit small) and a cost for resizing operations, which can be important in huge ArrayLists
with many insertions and deletions. Of course, this doesn't happen with arrays because by their very nature have a fixed size, that's both an advantage and a disadvantage.
Answering your question: if you know in advance the number of elements that you're going to need, and those elements aren't going to change much (insertions, deletion) then your best bet is to use an array. If some modification operations are needed and performance is of paramount importance, try using either Trove or fastutil.
Upvotes: 1
Reputation: 423
Retrieval of specific string,deleting specific string...i think ArrayList is not the best solution. Take a look at HashSet or LinkedHashSet.
Upvotes: 0
Reputation: 2503
an array is more efficient performance wise than an arraylist but unless you know how many elements you will be placing into an array an arraylist would be a better option since the size of the arraylist can grow as needed whereas a static array cannot.
Upvotes: 1
Reputation: 44706
Neither. If you want retrieval of specific strings (e.g. get the string "Foo") and deleting specific strings (e.g. delete "Foo"), I would consider using a Set
.
An array list or an array will give you O(N) retrieval (unless you keep it sorted). A Set
will typically give you at least O(lg N) time for finding a specific item.
Upvotes: 3
Reputation: 54074
ArrayList
is backed by an array so performance wise you should see no difference.
If there is no error in your requirements, and indeed you have to choose among only an arraylist and a raw array, I would suggest an arraylist since you have all the APIs to manipulate the data available which you would have to write yourself for a raw array of String
s.
Upvotes: 1