Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / October 2006

Tip: Looking for answers? Try searching our database.

Seeking a smarter idiom

Thread view: 
Eric Sosman - 03 Oct 2006 04:10 GMT
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...);

(If I were in the habit of storing null values in Maps, I'd
also need a containsKey() test.)  The last time I did this
the keys were host names and each value held a summary of the
traffic to and from its host: when a previously unknown host
entered the picture, a new record got created for it and
added to the Map.

    Fine and dandy, but something always irks me about this
idiom.  Take my photo and color it old-fashioned, but I can't
help noticing that the map.put() operation almost certainly
duplicates most of the work of the unsuccessful map.get() that
preceded it.  I'm working twice as hard as I "should" to insert
a new key/value pair, because map.put() cannot exploit the
information gained by map.get().  (At least, I don't think it
can: I haven't looked at the sources of HashMap and TreeMap,
but I find it hard to imagine how they could avoid two searches.)

    Given a suitable MutableType, the second search could be
avoided by making use of the value returned by map.put():

    Map<KeyType,MutableType> map = ...;
    ...
    KeyType key = ...;
    ValueType value = new Value(...info...);
    ValueType oldValue = map.put(key, value);
    if (oldValue != null)
       value.assimilate(oldValue);

This makes me itchy, too (perhaps unnecessarily).  The "obvious"
scheme is to create a blank record the first time a key appears
and then keep updating it as additional information arrives; the
approach above creates a new record every time a new driblet of
data comes in, throwing away the old one after incorporating its
goodies into the new.  Gives me goose pimples, it does.

    What idioms and patterns do other folks use for this sort of
thing?  It seems a pretty common task to want to accomplish;
surely someone's got a better idea ...?

Signature

Eric Sosman
esosman@acm-dot-org.invalid

Matt Humphrey - 03 Oct 2006 16:44 GMT
>     Here's something I find myself doing a lot:
>
[quoted text clipped - 7 lines]
> }
> 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@ivizNOSPAM.com http://www.iviz.com/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.