473,395 Members | 1,675 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Why ArrayList, not Hashtable

Hi
I am having a problem understanding the exact advantages of using an
ArrayList over a Hashtable in any situation.
In most areas of an application I am working on, lookup needs to be
fast. If I use a hashtable
Addition - O(1) - same as ArrayList
Removal - O(1) - same as ArrayList
Lookup- O(1) - ArrayList is O(n)
Memory - Maybe slightly more than ArrayList since we have a hash to
store.

And even if I need lookup by index which is easy using ArrayList, I
could simply use a Hashtable with the array index as the key and value
as my object.

The only other thing I do not know about is serialization efficiency.

So, my question is, anywhere I need lookup, I can just blindly use a
hashtable? In which situation would it be better to use an ArrayList?

Thanks
Bruce

Jul 8 '07 #1
2 2449
Hello Bruce,

BSo, my question is, anywhere I need lookup, I can just blindly use a
Bhashtable? In which situation would it be better to use an ArrayList?

Yep. the hashlist advantage over the arraylist in the way of getting value
by key

---
WBR, Michael Nemtsev [C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo
Jul 8 '07 #2
Bruce wrote:
Hi
I am having a problem understanding the exact advantages of using an
ArrayList over a Hashtable in any situation.
In most areas of an application I am working on, lookup needs to be
fast. If I use a hashtable
Addition - O(1) - same as ArrayList
Not true. The worst case is O(n). If you have a very bad hash, there
will be n hash collisions when you try to add your element to the
hashtable. (Hashtables in C# also grow automatically, and this may
involve copying all the data in the old hashtable. You get this problem
also with an arraylist, so that's O(n) too).
Removal - O(1) - same as ArrayList
The worst case is O(n). Again, if your hash is very bad.
Lookup- O(1) - ArrayList is O(n)
Again, it's O(n) in the worst case. (If your hash is very bad).
Memory - Maybe slightly more than ArrayList since we have a hash to
store.
The constant of proportionality, k, is likely to be *much* larger in the
case of the hashtable (that's the really big problem).

I think you should read up on O(...) notation, and how hashtables and
arraylists work. You'd probably find it more useful than the answers
you'll get by asking in newsgroups.

Alun Harford
Jul 8 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Matthias Kwiedor | last post by:
Hi@all! I have a app (c#) where i load up a external dll (managed code from c#) with a small arraylist and hashtable in two routines (about 40000 objects in each arraylist and hashtable). If...
6
by: Dan V. | last post by:
I would like to create a 2D string list (2D ArrayList ???). I would like to pass in a table or query as a parameter and have both columns transform into a 2D ArrayList. When I sort the one...
5
by: Matthias Kwiedor | last post by:
Hi! What i want to do, is to put an ArrayList into a Hashtable. So i can access the ArrayList fast over the key-indexing of the Hashtable. What i did is to put the ArrayList into the...
5
by: Netmonster | last post by:
Hello, Does any one have an example of how to create an ArrayList of objects? i.e. ArrayList of ArrayLists or an ArrayList of Hashtables? Thanks in advance KC
11
by: paradox | last post by:
Basically I have an ArrayList of strings; I need a fast way of searching through and comparing the elements of the ArrayList and the return an ArrayList of items that have 3 Duplicates. For...
3
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList...
3
by: Sam | last post by:
Hi Everyone, I have a stucture below stored in an arraylist and I want to check user's input (point x,y) to make sure there is no duplicate point x,y entered (string label can be duplicated). Is...
10
by: chrisben | last post by:
Hi, Here is the scenario. I have a list of IDs and there are multiple threads trying to add/remove/read from this list. I can do in C# 1. create Hashtable hList = Hashtable.Synchronized(new...
7
by: Kamran Shafi | last post by:
Hi, I am creating an arraylist (say masterArrayList) of hashtables, where each hashtable (say table) is of the format key=string, value = arraylist of strings (say existing_strings). In a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.