I would argue that a Map
is actually a Set
!
Map<Key,Value>
can be implemented with Set<Entry<Key,Value>>
This is similar to the mathematical foundations of what sets, maps, and functions are.
Firstly, can we agree that a Map
is a function from Key=>Value
(or Domain=>Range
). Each key corresponds with at most one value, so it is a partial function (or a complete function only upon those keys in the map). So a map is a function. (Scala even goes so far as to have Map implement the Function1 interface.)
Secondly, what is a function? A function is a set of tuples where each first element occurs only once in the set. The second element of the tuple is the value returned by the function.
So we have Map is a Function is a Set
.
On a practical note, there are very good reasons for having Set
s. They are very often the correct data structure to use from a conceptual point of view, even before you start worrying about performance. I'd use them over a List
in most situations.