difference between arraylist and hashmap

what is the difference between arraylist and hashmap
LVL 9
Manikandan ThiagarajanSenior consultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

;JOOP!
Manikandan ThiagarajanSenior consultantAuthor Commented:
is it hashmap is synchronized?
hoomanvCommented:
The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.

http://www.mindprod.com/jgloss/arraylist.html 

http://www.mindprod.com/jgloss/hashmap.html
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.