> I would like to know whether iterating through the
> e ArrayList to get a particular element is faster or
> having an HashMap and getting the element using the
> key is faster ?
> Can you please let me know which has a better
> performance?
Usually, Hashmap is faster, unless you have a very short list.
The point is: when you need a list, a Hashmap won't help you.
Generally List implementations to indicate that they support fast
In that Arraylist,without iterator to reterive the list with the help of for loop but Hashmap is not possible to reterive the list without iterator.
Genrally Iterator behaviour is fails-fast.
and one more advantage in ArrayList is implements RandomAccess.
it is used to reterive the list fastly.
The power of Christ compells you! :P
If your Arraylist is ordered, you can probably implement a binary search algorithm, which will give you O(log n) performance. If your Arraylist is unsorted, you're looking at O(n) performance.
HashMap guarrantees a lookup performance of between O(1) and O(n) - O(1) would obviously be where you know what the key is, and O(n) being where you have no idea what the key is and are iterating over the values to find the value you want.
J