So your saying HashSet is really using HashMap under the covers. So does this mean that the [key, value] pair is a same to same relationship. For example in the case of a set of Strings:
Key Value
"apple" "apple"
"grape" "grape"
"cherry" "cherry"
Is that the way it works?
HashSet internally uses HashMap. To be more precise this is how it is maintained inside an HashMap object:
So when you add an element to HashMap using add(E o), this method internally calls map.put(o, PRESENT) method. And here PRESENT is an Object reference. Which goes like this:
So when you store "apple", "grape", "cherry" in your HashSet it gets stored in HashMap as
KEY VALUE
--------------
"apple" PRESENT
"grape" PRESENT
"cherry" PRESENT
--------------