Learn how to a build a cloud-first strategyRegister Now


difference between arraylist and hashmap

Posted on 2006-04-22
Medium Priority
Last Modified: 2008-01-09
what is the difference between arraylist and hashmap
LVL 24

Assisted Solution

sciuriware earned 100 total points
ID: 16517852
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.


Author Comment

by:Manikandan Thiagarajan
ID: 16517975
is it hashmap is synchronized?
LVL 14

Expert Comment

ID: 16518131
The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Accepted Solution

kjetijo earned 100 total points
ID: 16518767
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.
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 100 total points
ID: 16518808
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.



Author Comment

by:Manikandan Thiagarajan
ID: 16583472
difference between hashmap and hashtable
LVL 30

Expert Comment

by:Mayank S
ID: 16583525
HashMap is not synchronized so it is faster. Hashtable is synchronized so it is thread-safe. Same as ArrayList vs Vector.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month21 days, 7 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question