Use Map
for key-value pairing
For key-value tracking, use Map
implementation.
For example, tracking which person is covering which day of the weekend. So we want to map a DayOfWeek
object to an Employee
object.
Map < DayOfWeek , Employee > weekendWorker =
Map.of(
DayOfWeek.SATURDAY , alice ,
DayOfWeek.SUNDAY , bob
)
;
When choosing one of the Map
implementations, there are several aspects to consider. These include: concurrency, tolerance for NULL values in key and/or value, order when iterating keys, tracking by reference versus content, and convenience of literals syntax.
Here is a chart I made showing the various aspects of each of the ten Map
implementations bundled with Java 11.
