How to use a Map the right way



  • Hey,

    this is my first Post here, so: Hi ;)


    This is just a minor WTF, but I had a laugh, since this Code is from some - so I thought - good java coder:

    (sContext.getModel() returns a java.util.Map)

     

    String slotPlaceHolder = AbstractBuilder.getSlotPlaceholder(sContext.getModel());
    for (Iterator iter = sContext.getModel().keySet().iterator(); iter.hasNext();) {
        Object key = iter.next();

        if (!key.equals(slotPlaceHolder)) {
            continue;
        }
        Object o = sContext.getModel().get(key);

        //more Code that does something with "o"
    }




  • This may be a crude, awful attempt at sidestepping the issue of Map.get() returning null both for the case when there is no mapping for the given key, and for the case when the given key is actually mapped to null. This 'more Code that does something with "o"' will be executed if the key is mapped to null, but not if there is no mapping for it.

    Of course, the right way to do this is to use the Map.containsKey() method.



  • Another funny thing is, there's no break.

    The loop always iterates through each and every key in that Map, even if the required key has been found.. ;)

     

     



  • @lupo said:

    Another funny thing is, there's no break.

    The loop always iterates through each and every key in that Map, even if the required key has been found.. ;)

     

     

    That's easy to explain, though. You noticed how, when you're trying to find something, it's always in the last drawer you look into?



  • @Ilya Ehrenburg said:

    @lupo said:

    Another funny thing is, there's no break.

    The loop always iterates through each and every key in that Map, even if the required key has been found.. ;)

     

     

    That's easy to explain, though. You noticed how, when you're trying to find something, it's always in the last drawer you look into?

    Not for me.  I continue to check all the other drawers after finding it, just in case.


  • @bstorer said:

    Not for me.  I continue to check all the other drawers after finding it, just in case.

     

    Admission of guilt? Or are you just paranoid?



  • I've seen this in my Learning Perl classes too, occasionally. In Perl, it'd be something like:

    my $key = "something";
    foreach (keys %somehash) {
    next unless $_ eq $key;
    my $value = $somehash{$key}; # and here's the WTF... the whole loop can be replaced by this
    ..
    }
    I guess some people just don't get the notion of a dictionary doing this for you already.

  • ♿ (Parody)

    @realmerlyn said:

    I guess some people just don't get the notion of a dictionary doing this for you already.
    If you didn't watch it happen, did it really happen?  How can you trust a runtime library?



  • @Ilya Ehrenburg said:

    @bstorer said:

    Not for me.  I continue to check all the other drawers after finding it, just in case.

     

    Admission of guilt? Or are you just paranoid?

     

    I call it being thorough.  Everyone assumes that objects in the universe only exist in one place, but how will we ever know for sure if we always give up looking for more instances after we've found just the first one?

    Perhaps the universe's code does something like this:

    Object lostObject = // some lost object

    Drawers[drawerIndex].add(lostObject);

    Drawers[drawerIndex+1].add(lostObject); // ha ha, they'll never think to look for it in the next drawer.

     

    This also supports my theory of why people "lose" socks in the laundry.  A pair of socks is really just 2 references to the same instance of sock.  When you put them in the laundry sometimes the universe's garbage collector will clean up one of the references.



  • @Apprentice of Dr. Wily said:

    @Ilya Ehrenburg said:

    @bstorer said:

    Not for me.  I continue to check all the other drawers after finding it, just in case.

     

    Admission of guilt? Or are you just paranoid?

     

    I call it being thorough.  Everyone assumes that objects in the universe only exist in one place, but how will we ever know for sure if we always give up looking for more instances after we've found just the first one?

    Perhaps the universe's code does something like this:

    Object lostObject = // some lost object

    Drawers[drawerIndex].add(lostObject);

    Drawers[drawerIndex+1].add(lostObject); // ha ha, they'll never think to look for it in the next drawer.

    True.  Plus, some drawers are just symbolic links to other drawers.


  • @bstorer said:

    Plus, some drawers are just symbolic links to other drawers.

    Like how my drawers are really just linked to your mom's pants?



  • @morbiuswilters said:

    @bstorer said:

    Plus, some drawers are just symbolic links to other drawers.

    Like how my drawers are really just linked to your mom's pants?

    Wait, did you just admit to wearing women's underwear?


  • @Apprentice of Dr. Wily said:

    This also supports my theory of why people "lose" socks in the laundry

     

    This happens because they have to sacrifice them to the God of Laundrette. Obligatory Eddie Izzard reference.


Log in to reply