2

I was reading an article in which they explained the JAVA HashSet uses HashMap internally as a backing store. So question arrises, Why don't they have separate implementation for HashSet as keeping HashMap requires {Key, Value}, However HashSet requires only {Key}. Doesn't this type of implementation wastes double the memory?

sgrpwr
  • 431
  • 3
  • 12
  • 1
    It can waste ~4 bytes per entry. However as a percentage it doesn't make so much difference, about 4 in 32 bytes per entry (including the array in the HashMap) – Peter Lawrey Jul 16 '18 at 18:48

0 Answers0