Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 iterate over it every time you need to get at something. Or is there an easier way to do this?

like image 604
harley Avatar asked Dec 02 '25 11:12

harley


2 Answers

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.

like image 131
brabster Avatar answered Dec 05 '25 01:12

brabster


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?

like image 45
Joachim Sauer Avatar answered Dec 05 '25 00:12

Joachim Sauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!