Iterating in Java



  • In a custom servlet on the ecommerce site I maintain, here's some special list iteration I recently found. It's not major (the code is correct for a single use case), but it's still wrong.

    final List<RepostoryItem> items = (List<RepositoryItem>) request.getParameter(PARAM_NAME);
    Iterator<RepositoryItem> iterator = list.iterator();
    RepositoryItem target = null;
    
    for (int i = 0; i < list.size(); i++) {
        RepositoryItem item = (RepositoryItem) iterator.next();
        Boolean attr = (Boolean) currentItem.getPropertyValue("attr");
        if (attr != null && attr.booleanValue()) {
            target = item;
            break;
        }
    }
    
    ...
    

    I feel strangely compelled to look at this loop over and over again. I guess I'm trying to understand something that can't be understood?



  • I guess it's one of those typos that result from a combination of similar and meaningless names and autocompletion and/or copy-paste. The application is probably also so big that this error hardly ever occurs...



  • I shiver when I see a List casting, and he doesn't even use it.



  • @TGV said:

    I guess it's one of those typos that result from a combination of similar and meaningless names and autocompletion and/or copy-paste. The application is probably also so big that this error hardly ever occurs...
    What typo?  I was too busy noticing TRWTF that he gets an iterator to a list and then uses an int for-loop to iterate it as many times as list.size() rather than just using the iterator's hasNext() method to know how long to keep iterating.

    Edit: Oh, that typo.



  • @DaveK said:

    @TGV said:

    I guess it's one of those typos that result from a combination of similar and meaningless names and autocompletion and/or copy-paste. The application is probably also so big that this error hardly ever occurs...
    What typo?  I was too busy noticing TRWTF that he gets an iterator to a list and then uses an int for-loop to iterate it as many times as list.size() rather than just using the iterator's hasNext() method to know how long to keep iterating.

    Edit: Oh, that typo.

    I'm guessing the typo was a result of anonymization, or else this wouldn't even compile.



  • @morbiuswilters said:

    I'm guessing the typo was a result of anonymization, or else this wouldn't even compile.

    Unless [code]RepostoryItem[/code] is a sub-class of [code]RepositoryItem[/code].



  • @mikeTheLiar said:

    @morbiuswilters said:
    I'm guessing the typo was a result of anonymization, or else this wouldn't even compile.

    Unless <font face="Lucida Console" size="2">RepostoryItem</font> is a sub-class of <font face="Lucida Console" size="2">RepositoryItem</font>.

    I concede defeat. Your penis is fuller and more vibrant--with more stately plumage and a more pleasing aroma--than my own shriveled disgrace. I am turning over to you the contents of my harem and all of my land. Enjoy, m'lord.



  • @morbiuswilters said:

    @mikeTheLiar said:
    @morbiuswilters said:
    I'm guessing the typo was a result of anonymization, or else this wouldn't even compile.

    Unless <font face="Times New Roman" size="2">RepostoryItem</font> is a sub-class of <font face="Times New Roman" size="2">RepositoryItem</font>.

    I concede defeat. Your penis is fuller and more vibrant--with more stately plumage and a more pleasing aroma--than my own shriveled disgrace. I am turning over to you the contents of my harem and all of my land. Enjoy, m'lord.


    GODDAMNIT now you've gone and lost morbs again. It'll be another two years before he posts again!



  • @morbiuswilters said:

    I concede defeat. Your penis is fuller and more vibrant--with more stately plumage and a more pleasing aroma--than my own shriveled disgrace. I am turning over to you the contents of my harem and all of my land. Enjoy, m'lord.

    AND I AM BECOME LORD YOUR GOD, WIELDER OF THE FIERY PURPLE DILDO. I HOLD YOU ALL WITHIN THE WARMTH OF MY COLON. BEAR WITNESS TO MY GLORY AS I SPREAD MY MANY WINGS AND ERUPT TENTACLES FROM MY EYES, AND WITH THE FURY OF A THOUSAND DEATH ROW INMATES I FURIOUSLY YET GENTLY SODOMIZE YOUR LOVED ONES



  • This is why C# is better (in this respect anyway) with linq; all this boils down to one or two lines of code



    var items = request.getParameter(PARAM_NAME) as List<RepositoryItem>;

    var target =items.firstOrDefault(x => (x.getPropertyValue("attr") as bool?) == true);



    I think that would do it.



    Is it possible for attr to be null in java? or is it always a true/false value? In c# you only get true/false; you need bool? for true/false/null



    What is getPropertyValue? Is that just accessing the property 'attr'? In that case, the linq would be

    var target =items.firstOrDefault(x => x.attr);



  • @Ben L. said:

    @morbiuswilters said:
    @mikeTheLiar said:
    @morbiuswilters said:
    I'm guessing the typo was a result of anonymization, or else this wouldn't even compile.

    Unless <font face="Times New Roman" size="2">RepostoryItem</font> is a sub-class of <font face="Times New Roman" size="2">RepositoryItem</font>.

    I concede defeat. Your penis is fuller and more vibrant--with more stately plumage and a more pleasing aroma--than my own shriveled disgrace. I am turning over to you the contents of my harem and all of my land. Enjoy, m'lord.


    GODDAMNIT now you've gone and lost morbs again. It'll be another two years before he posts again!

    Still, on the bright side, it'll be another two years before he posts again!

     


  • Winner of the 2016 Presidential Election

    LINQ looks so wrong with camel case methods.



  • Whoops, sorry about the typo...

    Anyways, it's a special way to iterate. I've decided to add a comment, warning future devs against such behavior, but the code won't change.



  • @DrPepper said:

    Is it possible for attr to be null in java?

    Why, yes, yes it is! Just further proof that Java was designed by sadistic extraterrestrials to retard human technological progress in the hope of easing colonization!



  • @morbiuswilters said:

    @DrPepper said:
    Is it possible for attr to be null in java?

    Why, yes, yes it is! Just further proof that Java was designed by sadistic extraterrestrials to retard human technological progress in the hope of easing colonization!

    It's also possible for attr to be of a type other than boolean. attr could be a FileOutputStream for all we know.



  • @Ben L. said:

    @morbiuswilters said:
    @DrPepper said:
    Is it possible for attr to be null in java?

    Why, yes, yes it is! Just further proof that Java was designed by sadistic extraterrestrials to retard human technological progress in the hope of easing colonization!

    It's also possible for attr to be of a type other than boolean. attr could be a FileOutputStream for all we know.

    If we want to get really pedantic, no, attr is always Boolean. True, currentItem.getPropertyValue("attr") could return a non-Boolean type, but as soon as it tries to cast it to a Boolean you will get a ClassCastException; however, the static code clearly shows attr as Boolean.



  • @morbiuswilters said:

    easing colonization
     

    What do you mean by colonization.



  • @DrPepper said:

    This is why C# is better (in this respect anyway) with linq; all this boils down to one or two lines of code



    var items = request.getParameter(PARAM_NAME) as List;

    var target =items.firstOrDefault(x => (x.getPropertyValue("attr") as bool?) == true);



    I think that would do it.



    Is it possible for attr to be null in java? or is it always a true/false value? In c# you only get true/false; you need bool? for true/false/null



    What is getPropertyValue? Is that just accessing the property 'attr'? In that case, the linq would be

    var target =items.firstOrDefault(x => x.attr);

    Can you cast to a generic type without specifying the types? I don't know of a List class* that's not generic, and if it implemented IEnumerator and not IEnumerator<T>, you'd have to cast it.

    Also this is more a manner of style but I would have cast it via (List)request.getParameter(PARAM_NAME); rather then using the as operator. Otherwise, if for some reason the cast should fail, you'd have an InvalidCastException rather then a NullReferenceException.

    * by List class I mean a class named List. I know about System.Collections.ArrayList.



  • @dhromed said:

    @morbiuswilters said:

    easing colonization
     

    What do you mean by colonization.

    The aliens' genitals are made of pure, psychic energy, meaning no physical dongs. So it's not what you're hoping for.



  • @mikeTheLiar said:

    @morbiuswilters said:
    I concede defeat. Your penis is fuller and more vibrant--with more stately plumage and a more pleasing aroma--than my own shriveled disgrace. I am turning over to you the contents of my harem and all of my land. Enjoy, m'lord.

    AND I AM BECOME LORD YOUR GOD, WIELDER OF THE FIERY PURPLE DILDO. I HOLD YOU ALL WITHIN THE WARMTH OF MY COLON. BEAR WITNESS TO MY GLORY AS I SPREAD MY MANY WINGS AND ERUPT TENTACLES FROM MY EYES, AND WITH THE FURY OF A THOUSAND DEATH ROW INMATES I FURIOUSLY YET GENTLY SODOMIZE YOUR LOVED ONES

    Stop watching so much anime, Mike.
    Although I'm pretty sure this is exactly what morbs meant by 'colonization'.


  • @Ben L. said:

    attr could be a FileOutputStream for all we know.
    I'm sure you meant FileNotFoundStream.



  • @Anonymouse said:

    @Ben L. said:

    attr could be a FileOutputStream for all we know.
    I'm sure you meant FileNotFoundStream.

    public class FileNotFoundStream extends OutputStream {
        @Override
        public void write(int b) throws IOException {
            throw new FileNotFoundException();
        }
    }


  • @mikeTheLiar said:

    @morbiuswilters said:
    I concede defeat. Your penis is fuller and more vibrant--with more stately plumage and a more pleasing aroma--than my own shriveled disgrace. I am turning over to you the contents of my harem and all of my land. Enjoy, m'lord.

    AND I AM BECOME LORD YOUR GOD, WIELDER OF THE FIERY PURPLE DILDO. I HOLD YOU ALL WITHIN THE WARMTH OF MY COLON. BEAR WITNESS TO MY GLORY AS I SPREAD MY MANY WINGS AND ERUPT TENTACLES FROM MY EYES, AND WITH THE FURY OF A THOUSAND DEATH ROW INMATES I FURIOUSLY YET GENTLY SODOMIZE YOUR LOVED ONES

    That looks remarkably similar to the caption for the overwatch voice in the first chapter of HL2EP2. Don't ask why I know that.

Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.