r/ProgrammerTIL • • Oct 22 '17

Other [Java] HashSet<T> just uses HashMap<T, Object> behind the scenes

76 Upvotes

35 comments sorted by

View all comments

Show parent comment

8

u/kazagistar Oct 23 '17

Well, from a practical perspective, Rust does the same thing as Java, where HashSet<T> is implemented as HashMap<T, ()>, but since the unit type () is a zero sized type, it takes up no memory, and any code that would handle it is optimized out, leading to an implementation as efficient as a handcrafted one.