This repository demonstrates what happens when Java Map
s are instantiated with various key implementations, most of
which are silly and should probably never be used.
Review the tests and corresponding key implementations to see how map behavior changes.
-
StableMapKey
-- this map key is "stable" meaning it will alwaysget
you back what youput
and will allow the map to have generally expected performance characteristics (i.e.: O(1) lookup). Not silly. -
AlwaysCollideMapKey
-- this silly map key generally works fine, but will cause collisions on everyput
resulting in O(n)get
s. -
SingleEntryMapKey
-- this silly map key causes aMap
to behave like it can only support one entry. Eachput
will overwrite the existing entry. -
CantFindAnythingMapKey
-- this silly map key will cause the map to appear like it loses all its entries because it will causeget
s to returnnull
. The entries are there, however. Lookup is O(1) I guess, but you won't ever get the value you're looking for, even if you have a reference to the exact key instance that wasput
. -
UnstableMapKey
-- this silly map key seems OK, until you realize that it's mutable. If the key mutates after it's used toput
you won't be able to find the corresponding entry anymore.
./gradlew test