HashMap and ArrayList

Hi, I would like to know whether iterating through the 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?Thanks
[265 byte] By [scsca] at [2007-10-2 16:49:32]
# 1

> 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.

CeciNEstPasUnProgrammeura at 2007-7-13 18:00:50 > top of Java-index,Java Essentials,Java Programming...
# 2

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.

Ravikumara at 2007-7-13 18:00:50 > top of Java-index,Java Essentials,Java Programming...
# 3

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

jagulara at 2007-7-13 18:00:50 > top of Java-index,Java Essentials,Java Programming...
# 4
> The power of Christ compells you! :PWhat does that have to do with anything? And why does that smiley go with that statement?
doremifasollatidoa at 2007-7-13 18:00:50 > top of Java-index,Java Essentials,Java Programming...
# 5
> > The power of Christ compells you! :P> > What does that have to do with anything? And why> does that smiley go with that statement?It looked to me like he was exorcising Ravikumar's post...
yawmarka at 2007-7-13 18:00:50 > top of Java-index,Java Essentials,Java Programming...
# 6
> It looked to me like he was exorcising Ravikumar's> post...trying to, trying to...J
jagulara at 2007-7-13 18:00:50 > top of Java-index,Java Essentials,Java Programming...