How's about Collections.getSafe(...) for Map, which wouldn't return null? (original) (raw)
Paul Benedict pbenedict at apache.org
Mon Oct 26 14:56:10 UTC 2009
- Previous message: How's about Collections.getSafe(...) for Map, which wouldn't return null?
- Next message: How's about Collections.getSafe(...) for Map, which wouldn't return null?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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> map = ... List collection = map.get(key); if (collection == null) { collection = new LinkedList(); map.put(key, collection); } collection.add(value);
Paul
- Previous message: How's about Collections.getSafe(...) for Map, which wouldn't return null?
- Next message: How's about Collections.getSafe(...) for Map, which wouldn't return null?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]