Java HashSet key/value pair
Why doesn't Java provide functions to get the key/value pairs in a HashSet
like exists in Hashtable
? It seems like a real pain to have to itera开发者_Go百科te over it every time you need to get at something. Or is there an easier way to do this?
HashSet doesn't have key/value pairs. It is a Set of objects and you would use an implementer of Set to ensure that a collection of objects contained no duplicates.
Implementers of Map like HashMap have key/value pairs and provide a get(Object key) method to get the value associated with a key.
Since a Set
doesn't contain keys and values, there is no way such a view could be provided.
What would you consider to be the key and what would be the value in a Set
?
A Set
don't have any key/value pairs, just (unique) values. As you already said you get these values via the Iterator
or by returning an array with these values with the toArray()
method.
Maybe you are looking for a List
instead.
精彩评论