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<L1Risk, Map.Entry<int[], Map<L2Risk, Map.Entry<int[], Map<L3Risk, List<Event>>>>>>
getEventTable(RiskClassifier classifier) {
Map<L1Risk, Map.Entry<int[], Map<L2Risk, Map.Entry<int[], Map<L3Risk, List<Event>>>>>> l1s = new HashMap<>();
Map<L2Risk, Map.Entry<int[], Map<L3Risk, List<Event>>>> l2s = new HashMap<>();
Map<L3Risk, List<Event>> l3s = new HashMap<>();
List<Event> 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...