I have a HashSet<T> and I want to get the hashcode of the HashSet<T> based on the items it contains. I am trying to identify a HashSet<T> based on it's values. I know I can use set operations to check if two HashSet<T>'s are equal. I need a hashcode because I want to use memoization in a function which takes a HashSet<T> as an argument.
HashSet<string> one = new HashSet<string>();
one.Add("java");
Console.WriteLine(one.GetHashCode());
HashSet<string> two = new HashSet<string>();
two.Add("java");
Console.WriteLine(two.GetHashCode() == one.GetHashCode());
// I want the hash code to be same for one and two,
// since they both contain the same strings.
Is there a way to identify a HashSet<T> based on it's values without overriding the GetHashCode method? If there isn't one could you please provide the override implementation for GetHashCode of a HashSet<T> of strings?
The
HashSet<T>has a staticCreateSetComparermethod that could solve your problem if it worked correctly, but it doesn't.This will print
truein your example, but if the two hashsets are configured with a non-default comparer, for exampleStringComparer.OrdinalIgnoreCase, and contain items that are different according to the default comparer, for example"java"and"Java", it will printfalse.The
HashSetEqualityComparer<T>class below is aware of theComparerof each hashset, and produces identical hashcodes for hashsets that contain the same elements, regardless of the order that they were inserted in the hashsets.Usage example:
The
HashCodeis a built-instruct, that is used for combining multiple values into a single hashcode.Sorting the hashcodes of the contained elements is a relatively expensive O(n log n) operation. In case paying this performance cost is not desirable, you can get a valid hashcode faster using the XOR technique:
The quality of the resulting hashcode might not be as good though. The
HashCode.Combinemethod with a single argument can be used in order to diffuse the hashcode of each element across a large range. Which improves the quality of the resulting hashcode in case, for example, that the elements are small integer numbers.