Hashtable / Dictionary, but with a key consisting of several values?

Lets say that I have an object that has stringProp1, stringProp2. I want to save every combination of stringProp1, stringProp2 in a dictionary. Initially, I saved the key as key = stringProp1 + stringProp2, but this can cause an error depending on two values. The best solution to this problem for creating a custom dictionary class, or the best way to use the built-in .NET classes?

+3
source share
6 answers

It doesn’t matter what data structure you use as a key, as long as the key stores the necessary information for comparison with the corresponding comparison / hash.

, , EqualityComparer. :

class MyObject
{
    public string StringProp1 { get; set; }
    public string StringProp2 { get; set; }
    public MyObject(string prop1, string prop2)
    {
        StringProp1 = prop1;
        StringProp2 = prop2;
    }
}

class MyObjectComparerS1S2 : EqualityComparer<MyObject>
{
    //Change this if you need e.g. case insensitivity or 
    //culture-specific comparisons
    static StringComparer comparer = StringComparer.Ordinal;

    public override bool Equals(MyObject x, MyObject y)
    {
        return 
            comparer.Equals(x.StringProp1, y.StringProp1) &&
            comparer.Equals(x.StringProp2, y.StringProp2);
    }

    public override int GetHashCode(MyObject obj)
    {
        //Uncomment this if running in a checked context
        //Copycat of Jon Skeet string hash combining
        //unchecked
        //{
            return 
                (527 + comparer.GetHashCode(obj.StringProp1)) * 31 +
                comparer.GetHashCode(obj.StringProp2);
        //}
    }

    public static readonly MyObjectComparerS1S2 Instance = 
        new MyObjectComparerS1S2();

}

static void Main(string[] args)
{
    Dictionary<MyObject, MyObject> dict = 
        new Dictionary<MyObject, MyObject>(MyObjectComparerS1S2.Instance);
    MyObject obj = new MyObject("apple", "plum");
    dict.Add(obj, obj);
    MyObject search = new MyObject("apple", "plum");
    MyObject result = dict[search];
    Console.WriteLine("{0}:{1}", result.StringProp1, result.StringProp2);
}

, , . , , @Vlad . EqualityComparer<MyKeyStructOrClass>.

, Jon Skeet . , XOR, MSDN. , , - Hsieh, Murmur, . -, #.

+1

.NET 4 System.Tuple .

var dict = new Dictionary<Tuple<string,string>, int>();
dict.Add(Tuple.Create("foo","bar"), 1);
+3

? .

+1

, , . , Person:

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}

, IEnumerable<Person> , , :

var personDictionary = people.ToDictionary(p => new { p.FirstName, p.LastName });

Person . :

personDictionary.TryGetValue(new { FirstName = "John", LastName = "Smith" },
    out person);

, , , . , GroupBy group by.

+1

, , .

,

stringProp1 + "|" + stringProp2

, Dictionary<string, Dictionary<string, MyValueType>>,

var dictionary = new Dictionary<string, Dictionary<string, MyValueType>>();
// .... Do stuff
if (!dictionary.ContainsKey(stringProp1))
    dictionary.Add(stringProp1, new Dictionary<string, MyValueType>());
dictionary[stringProp1][stringProp2] = myValue;
0

MD5 , . .
.NET MD5CryptoServiceProvider System.Security.Cryptography. ComputeHash -.

0

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


All Articles