CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2003
    Posts
    47

    Exclamation HashTable question

    Hi :
    Java's implementation of Hashtable provides the users with the public Object get(Object key)So if the user wants to retrieve the object value in the existing HashTable, they just need to provide the key, and the operation time is O(1).

    However, I don't see the similar method in C#, it only has getEnumerator which will go through the entire table. and the timing is O(N)

    I would like to know if there is something similar to the get(Object key) method I can use and has a time of O(1).


    Many thanks,
    ------------
    I.B.M... I Blame Microsoft

  2. #2
    Join Date
    May 2004
    Location
    Greenville, NC
    Posts
    193

    Re: HashTable question

    The following code will print 17 in constant amount of time...I think. I'm pretty sure it's a requirement that retrievals from a Hashtable take O(1) time, else I'm not really sure what distinguishes a HashTable...

    Code:
    Hashtable ht = new Hashtable();
    ht.Add("Jason", 23);
    ht.Add("Josh", 23);
    ht.Add("Jacob", 7);
    ht.Add("Jeremy", 17);
    
    System.Console.WriteLine(ht["Jeremy"]);
    Jason Isom
    .NET 2.0 / VS2005 Developer

  3. #3
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: HashTable question

    hashtable has a special enumerator IDictionaryEnumerator for obtaining / iterating through a list of keys or dict entries, and the actual get(key) function uses normal hashing to obtain the value.
    Last edited by MadHatter; October 16th, 2005 at 11:31 PM.

  4. #4
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074

    Re: HashTable question

    ...and the same goes for ArrayList. Java uses list.get(index) whereas C# uses the array syntax, list[index]
    Useful? Then click on (Rate This Post) at the top of this post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured