C# ArrayList Vs Hashtable

Posted on 2007-07-24
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
    LVL 8

    Expert Comment

    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

    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

    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?

    LVL 10

    Expert Comment


    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
    LVL 6

    Expert Comment

    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)
    LVL 3

    Expert Comment

    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.
    LVL 9

    Expert Comment

    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

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

    Assisted Solution

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now