Map.Entry
Java 1.6 and upper have two implementation of Map.Entry interface pairing a key with a value:
- AbstractMap.SimpleEntry
- AbstractMap.SimpleImmutableEntry
For example
Map.Entry < Month, Boolean > pair =
new AbstractMap.SimpleImmutableEntry <>(
Month.AUGUST ,
Boolean.TRUE
)
;
pair.toString(): AUGUST=true
I use it when need to store pairs (like size and object collection).
This piece from my production code:
public Map>>>>>
getEventTable(RiskClassifier classifier) {
Map>>>>> l1s = new HashMap<>();
Map>>> l2s = new HashMap<>();
Map> l3s = new HashMap<>();
List events = new ArrayList<>();
...
map.put(l3s, events);
map.put(l2s, new AbstractMap.SimpleImmutableEntry<>(l3Size, l3s));
map.put(l1s, new AbstractMap.SimpleImmutableEntry<>(l2Size, l2s));
}
Code looks complicated but instead of Map.Entry you limited to array of object (with size 2) and lose type checks...