Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there a separate equals method for sets?

Tags:

c#

equals

In C Sharp .NET there is a Equals method and a SetEquals method. Where is the difference?

Coming from Java, my first thought was that SetEquals is not necessary, just use the Equals method for all objects.

like image 595
John Threepwood Avatar asked Jun 20 '13 14:06

John Threepwood


People also ask

How do you know if two sets are equal?

The equals() method of java. util. Set class is used to verify the equality of an Object with a Set and compare them. The method returns true if the size of both the sets are equal and both contain the same elements.

How do you compare two sets of elements?

So, the equals() method is one of the most used and fast ways to compare two sets in Java. The equals() method compares two sets based on the type of the elements, size of the set, and value of the elements.

Does HashSet have an overloaded equals method?

You can use a Set to get rid of the duplicate entries, but beware: HashSet doesn't use the equals() methods of its containing objects to determine equality.

How does the equals method work in Java?

equals() Method. In Java, the String equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are the same, it returns true. If all characters are not matched, then it returns false.


1 Answers

SetEquals doesn't obey the same contract as Equals. In particular it's not symmetric, as the argument is just IEnumerable<T> rather than ISet<T>. This allows you to check for set equality while only having one set. Consider:

List<int> intList = new List<int> { 1, 2, 3 }; HashSet<int> intSet = new HashSet<int>(intList); 

Now we can use:

Console.WriteLine(intSet.SetEquals(intList)); 

... but we couldn't implement Equals in the same way without enforcing the same behaviour on List<int> and every other IEnumerable<T> implementation.

Even if we restricted it to other sets, there's the interesting question of what equality really means. For example, consider two HashSet<string> sets which contain the same strings, but have different equality comparers. (Maybe one is case-sensitive and one isn't.) Are those equal, or not? SetEquals manages to avoid such philosophical questions by avoiding trying to be too general.

What about a HashSet<int> and a SortedSet<int>? Could they ever be equal? They can have the same values - but the ordering of one is undefined.

Overall, the ideas of Object.Equals and Object.GetHashCode are too broad in my view. Often you want a particular type of equality - and often it makes no sense to compare objects for equality in the first place. This could easily form the subject of a completely different rant, but in the meantime I'm at least glad that .NET didn't try to apply this overly-broad idea to collections. The ability to use SetEquals with a well-defined meaning is more useful, IMO.

like image 121
Jon Skeet Avatar answered Oct 08 '22 00:10

Jon Skeet