They are special pairs like that because in a map, keys are unique. A set of classical pairs would have unique pairs, which would allow for same key with multiple values.
Yeah. That's not how HashSets work. E.g. if the element (perhaps, a pair, special or not) is not in the set, you can add it and if you do other elements are not affected and so on. Which is probably the reason HashSets are backed by HashMaps, not the other way around.
I have no idea what you're trying to say with that because even HashMap implementation keeps key-element pairs internally, placed into buckets or not by only hashing the key. The pairing is only necessary to hold the extra non-hashed related value, without it is a perfectly fine HashSet.
If you'd try to implement a HashMap as a set (perhaps a HashSet) of pairs, you'd either want operations that are not there or you'd have suboptimal performance. Suppose you add a key-value pair, you'd want to see if the key is in the map. The set doesn't allow you to check that efficiently. You can check if something is contained in a set quickly, but not if something is a member of an element of the set.
In a language like Java, the map entry class can implement hashCode by delegating to the key of the pair, making it exactly as efficient. You don't seem to be familiar with this.
It is the mathematical construction of HashMap though. Even when implementing HashMap directly this is done, only maybe through custom methods, not standard methods.
Working with abstractions, this is nothing unusual that one datatype has multiple possible behaviors that can be equally well used. In Haskell, the are, for example, multiple ways to abstract lists to be Applicative (default list instance, ZipList) or multiple ways for numbers to be Monoid (Sum, Product). It's similar with a pair type that it's equality may be abstracted differently for different use, there's no right way of defining everything. It takes getting used to this, working with abstractions like this.
While you might not be comfortable with an implementation via alternate pair definition, I might not be comfortable with the initial definition of HashSet, which wastes more heap than it really needs to, that has an actual runtime cost unlike an internal pair being defined in a non-standard way. Data structure internals are never elegant and beautiful, especially in practice, as they often involve multiple optimization hacks etc, even the standard ones in Java, nothing elegant or approvable inside those either if you really start to look.
View all comments
Show parent comment
2
u/an_actual_human Oct 23 '17
So that would not really be a
HashSetof pairs.