Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Hashtables/Dictionaries with string keys & Case Insensitive Searching

Wondering if this is possible.

We have an 3rd Party library that contains identification information about users...

The main interaction with the library is through a HashTable which is keyed with a string, and returns an Object Graph of information for that key.

The problem is, the key is obviously Case Sensitive, but what we get from the users browser doesn't necessarily match the case... (We often get the key fully lowercase'd)

I'm wondering if it's possible to do a case Insensitive key search against a hashtable.

e.g.

Hashtable ht = new Hashtable();
ht.Add("MyKey", "Details");

string result = ht["MyKey"];
string result = ht["MYKEY"];
string result = ht["mykey"];

On the off chance we could submit a support ticket to the company to add this functionality, are there any other DataStructures (i.e. the new generic collections/dictionaries) that support this functionality

Lastly, would it be possible to override the System.String GetHashCode() method, to make all case invariant strings return the same hashcode... e.g. I'm thinking this is a no goer as string is a sealed class

Cheers if anyone has any suggestions

like image 640
Eoin Campbell Avatar asked May 13 '09 09:05

Eoin Campbell


People also ask

When should you not use Hashtables?

There are some operations which are not efficiently supported by hash tables, such as iterating over all the elements whose keys are within a certain range, finding the element with the largest key or smallest key, and so on. The O(n) complexity is on average.

Are Hashtables and dictionaries the same?

A dictionary is a data structure that maps keys to values. A hash table is a data structure that maps keys to values by taking the hash value of the key (by applying some hash function to it) and mapping that to a bucket where one or more values are stored.

Which is faster dictionary or Hashtable?

Dictionary is a generic type and returns an error if you try to find a key which is not there. The Dictionary collection is faster than Hashtable because there is no boxing and unboxing.

Are dictionaries Hashtables?

Hashtable and Dictionary are collection of data structures to hold data as key-value pairs. Dictionary is generic type, hash table is not a generic type. The Hashtable is a weakly typed data structure, so you can add keys and values of any Object Type to the Hashtable.


2 Answers

Code to make the hashtable comparisons case-insensitive

For 2.0, 3.0, 3.5

Hashtable ht = new Hashtable(StringComparer.InvariantCultureIgnoreCase);

You can get info on InvariantCultureIgnoreCase vs. OrdinalIgnoreCase on this SO link

OR

Hashtable ht = System.Collections.Specialized.CollectionsUtil.CreateCaseInsensitiveHashtable();

Because case-insensitive dictionary collection is such a common use, the .NET Framework has a CollectionUtil class that supports creating Hashtable and SortedList objects that are case insensitive. Use by calling CreateCaseInsensitiveHashtable or CreateCaseInsensitiveSortedList.

For .Net 1.0 (I am not sure if 1.0 supports StringComparer)

public class InsensitiveComparer : IEqualityComparer
{
    CaseInsensitiveComparer _comparer = new CaseInsensitiveComparer();
    public int GetHashCode(object obj)
    {
        return obj.ToString().ToLowerInvariant().GetHashCode();
    }

    public new bool Equals(object x, object y)
    {
        if (_comparer.Compare(x, y) == 0)
        {
            return true;
        }

        else
       {
           return false;
       }
    }
}

Hashtable dehash = new Hashtable(new InsensitiveComparer());
like image 100
SO User Avatar answered Sep 29 '22 00:09

SO User


With a dictionary:

new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

but simpler, I believe StringDictionary is case-insensitive too:

    StringDictionary ht = new StringDictionary();
    ht.Add("MyKey", "Details");

    string result1 = ht["MyKey"];
    string result2 = ht["MYKEY"];
    string result3 = ht["mykey"];
like image 32
Marc Gravell Avatar answered Sep 29 '22 00:09

Marc Gravell