[Last Call] Learn how to a build a cloud-first strategyRegister Now


C# ArrayList Vs Hashtable

Posted on 2007-07-24
Medium Priority
Last Modified: 2008-01-09
Hi guys,

I was thinking of using C# ArrayList to group and sort 800,000 records.. Comparing with a hashtable, which is more efficient, someone told me once Arraylist reaches 100,000 or more, it gets slowed down or something or gets inefficient.. Anyone knows the difference in terms of performance for both?

Question by:jedistar

Expert Comment

ID: 19561949
It depends. Item insert efficiency? Item lookup efficiency? Item lookup by
index? Iten lookup by key? Item delete efficiency? Serialization efficiency?
LVL 15

Accepted Solution

Solar_Flare earned 500 total points
ID: 19561967
a hashtable is better for looking up values based on the key, but you can't really sort it - it maintains its own 'internal sort' to optmize the lookups.

so, if you want to lookup values then it's better to use a hashtable, if you want to sort all the items into a particular order then use the arraylist.
LVL 22

Expert Comment

ID: 19562097
jedistar - Do you have a dummy file that large, or a way to create one? Also, will it be coming in from one large file, or some number of smaller ones that you need to merge?

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 10

Expert Comment

ID: 19562585

Arraylist is a collection of objects(may be of different types).
Arraylist is very much similar to array but it can take values of different datatypes.
If you want to find something in a arraylist you have to go through each value in arraylist, theres no faster way out.

Hashtable is also collection which takes a key corresponding to each values.
If you want to find something in a hashtable you dont have to go through each value in hashtable, instead search for key values and is faster.

Thanks & Regards

Expert Comment

ID: 19563063
if you are using .net 2.0 you also should have a look at dictionay. it is a mix between hashtable (sort and identify objects by a key) and an arraylist (objects of a specified type).

for your application - it really depends on what you want to do with the data:
just store and sort them - use arraylist
pick specific objects out of a collection - use dictionary (or hashtable)
or both - use dictionary (or hashtable)

Expert Comment

ID: 19563378
Well, since get and set actions in an array are always constant time; I would say use an ArrayList. If you just had a really big set of data and you only needed to iterate or get values from it, then a hashtable would probably have been faster.

Expert Comment

ID: 19563528
As implied above the performance depends on what it is you want to do with the collection...
for example, inserting items into an ArrayList is probably quicker than inserting them into a Hashtable - but frequent key based lookups will be quicker with a Hashtable.
> If you want to find something in a arraylist you have to go through each value in arraylist,
> theres no faster way out.

this is not true - if you know something about the ordering of an ArrayList - you can use that information - for example ArrayList has the BinarySearch method for use on sorted lists.

Author Comment

ID: 19585525
Jim, one large file that need to be merged
LVL 22

Assisted Solution

JimBrandley earned 500 total points
ID: 19634738
Sorry about the delay - I was on vacation for a week.  As SolarFlare mentioned, a hashtable is pretty efficient for looking up a single value, but is not useful for sorts.

I think you are back to where we were with your previous question. Writing the IComparer and using the ArrayList to do the sort is your best bet.

Since this file is much larger, you may run into memory issues. If so, I would recommend breaking the file up in one pass that creates new files based on count. So read the first 80K records, sort them, but do not assign groups yet,  and write them to a temp file. Then close the output and create a new one, and read the next 80K from the input file and write to the second temp file. Repeat until you are done.

Now all the temp files are sorted. Next, open all the temp files,
Read one record from each. Compare those you just read, and find the smallest from that set of 10 records. Add the group number (as before) and write that record only to the final output file. Read the next record from the file from which you got the smallest. Repeat this process, incrementing group number as necessary until all records from the temp files have been sent to the final output file. Then delete the temp files.

This is an algorithm we used in the early 70s when non-volatile memory meant 9 track mag tapes.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

831 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