Retrieve a value from a key clone hash table; WITH#

I would like to know if there is any possible way to get an item from a hash table using a key that is identical to the actual key, but to a different object. I understand why this is probably not possible, but I would like to see if there is any complicated way to do this.

My problem arises from the fact that, being as stupid as I was, I created hash tables with int [] as keys with whole arrays containing indices representing the spatial position. I somehow knew that I needed to create a new int [] every time I wanted to add a new record, but I didnโ€™t think that when I create spatial coordinate arrays later, they would be useless in extracting values โ€‹โ€‹from my hash tables .

Now I'm trying to decide if I need to reorder things so that I can store my values โ€‹โ€‹in ArrayLists or look in the key list in the Hashtable for the one that I need every time I want to get a value, no options are really cool.

Unless, of course, there is a way to make // 1 work like // 2!

Thanks in advance.

 static void Main(string[] args)
        {
            Hashtable dog = new Hashtable();

            //1
            int[] man = new int[] { 5 };
            dog.Add(man, "hello");
            int[] cat = new int[] { 5 };
            Console.WriteLine(dog.ContainsKey(cat)); //false


            //2
            int boy = 5;
            dog.Add(boy, "wtf");
            int kitten = 5;
            Console.WriteLine(dog.ContainsKey(kitten)); //true;


        }
+3
source share
1 answer

Yes, you have two options.

  • Pipette Equals()and GetHashCode()in key classes.

  • Create a custom one IEqualityComparerand pass it to the constructor Hashtable.

Equals() GetHashCode() , . , IEqualityComparer, , . .

+8

Source: https://habr.com/ru/post/1737456/


All Articles