Getting an element from a Set

To answer the precise question “Why doesn’t Set provide an operation to get an element that equals another element?”, the answer would be: because the designers of the collection framework were not very forward looking. They didn’t anticipate your very legitimate use case, naively tried to “model the mathematical set abstraction” (from the javadoc) and simply forgot to add the useful get() method.

Now to the implied question “how do you get the element then”: I think the best solution is to use a Map<E,E> instead of a Set<E>, to map the elements to themselves. In that way, you can efficiently retrieve an element from the “set”, because the get() method of the Map will find the element using an efficient hash table or tree algorithm. If you wanted, you could write your own implementation of Set that offers the additional get() method, encapsulating the Map.

The following answers are in my opinion bad or wrong:

“You don’t need to get the element, because you already have an equal object”: the assertion is wrong, as you already showed in the question. Two objects that are equal still can have different state that is not relevant to the object equality. The goal is to get access to this state of the element contained in the Set, not the state of the object used as a “query”.

“You have no other option but to use the iterator”: that is a linear search over a collection which is totally inefficient for large sets (ironically, internally the Set is organized as hash map or tree that could be queried efficiently). Don’t do it! I have seen severe performance problems in real-life systems by using that approach. In my opinion what is terrible about the missing get() method is not so much that it is a bit cumbersome to work around it, but that most programmers will use the linear search approach without thinking of the implications.

Leave a Comment