How's about Collections.getSafe(...) for Map<?, ?>, which wouldn't return null?

Paul Benedict pbenedict at apache.org
Mon Oct 26 14:56:10 UTC 2009


Since we're talking Map, I think a more utility would be gained by
including putIfAbsent (see java.util.concurrent) because I see many
lines of code that do that idiom all the time.

This idiom is especially prevalent when the value is itself a nested collection.

Map<Object, List<Object>> map = ...
List<Object> collection = map.get(key);
if (collection == null) {
  collection = new LinkedList<Object>();
  map.put(key, collection);
}
collection.add(value);

Paul



More information about the core-libs-dev mailing list