Re: Seeking a smarter idiom
- From: "Matt Humphrey" <matth@xxxxxxxxxxxxxx>
- Date: Tue, 3 Oct 2006 11:44:08 -0400
"Eric Sosman" <esosman@xxxxxxxxxxxxxxxxxxx> wrote in message
news:NvidnWO0rsgCSbzYnZ2dnUVZ_o6dnZ2d@xxxxxxxxxxxxxx
Here's something I find myself doing a lot:
Map<KeyType,MutableType> map = ...;
...
KeyType key = ...;
MutableType value = map.get(key);
if (value == null) {
value = new MutableType();
map.put(key, value);
}
value.update(...info...);
<snip problem details>
I use this form alot for data structures that build themselves as data
arrive and when you can't really know the keys in advance. The issue is that
when you have the map-specific insertion information, you can't invoke the
value-type constructor. But if you could extend Map just a little...
Map.Entry mapEntry = map.getOrCreateEntry (key);
if (mapEntry.getValue () == null) {
mapEntry.setValue (new MutableType ())
}
mapEntry.getValue().update (...info...);
Map.Entry is an interface so it could contain map-specific insertion
information and wouldn't have to be a real node in the map structure.
Matt Humphrey matth@xxxxxxxxxxxxxx http://www.iviz.com/
.
- References:
- Seeking a smarter idiom
- From: Eric Sosman
- Seeking a smarter idiom
- Prev by Date: native threads in 1.4 on linux
- Next by Date: Beginner's Question: "*.java uses unchecked or unsafe operations"
- Previous by thread: Seeking a smarter idiom
- Next by thread: Beginner's Question: Create an identical object.
- Index(es):