difference between arraylist and hashmap

what is the difference between arraylist and hashmap
Manikandan ThiagarajanSenior consultantAsked:
Who is Participating?
The arraylist puts all your elements into an array contained within the variable. When accessing members, you generally have to run through the entire array to find it (unless you specify the position with an index instead of an object). So if you want to get 'Object A' out of the list, you have to check if it is in position 0 then return it if it is the one we're looking for, if not we check position 1 and return it if it's the one, if not we check position 2, etc. However this is not a static array and it will expand if it needs to be larger. This is all done by the class for you, so you generally don't have to think about it.

The HashMap works in a quite different way. It utilises a key to get the index of the array. Basically what it does is that it generates a key, usually a large odd number. Then it generates a quite large array to put your elements in. When deciding where to put your element (with the add-mehotd) they take the modulus of your object's id (how the id of each object is assigned, I don't know, but it always is a number), and then assign the element to that index. So, when we need to get 'Object A' back out, we just take Object A's id, and modulus it with the HashKey, and we get the index right out, and don't need to search through the entire array.

The HashMap, does however have it's weaknesses. Since you want to be able to have a large amounts of data stored in it, you need to have a large key, and a large array. This takes up alot of memory especially when you most of the indexes will remain empty. And you also have the off chance of to different objects generating the same index when they are modulus'ed with the Hash-Key. This is solved quite elegant, by just putting a LinkedList with objects at that index. It segnificantly reduces the searchtime, since it is very rare that two objects arrive at the same index, and you'll generally just have to check one-three objects on that index instead of the entire array, like you do with ArrayList.

So to sum up. You want to use HashMap when you know you're going to get large amounts of data, and you want to ascess it quickly. If you got small amounts of data, you want to use ArrayList since it takes up much less memory.
An ArrayList is a collection that has a hidden array that is secretly extended on need.

A HashMap is a collection that tries to give access to all contents by their hash-key.

The first is indexed, the second is not.
The first must be searched entirely for a member, the second should be accessible in one step.

Manikandan ThiagarajanSenior consultantAuthor Commented:
is it hashmap is synchronized?
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.
Mayank SAssociate Director - Product EngineeringCommented:
This question will be better answered if you understand the difference between a list and a map. A list is just a list of values whereas a map holds pairs of values (associated with key-value relationships). ArrayList is just an implementation of the List interface and HashMap is an implementation of the Map interface. Both are non-synchronized.


Manikandan ThiagarajanSenior consultantAuthor Commented:
difference between hashmap and hashtable
Mayank SAssociate Director - Product EngineeringCommented:
HashMap is not synchronized so it is faster. Hashtable is synchronized so it is thread-safe. Same as ArrayList vs Vector.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.