简体   繁体   中英

Coverting for loop to lambda and streams

I have this block of code and have spent some time trying to convert this for loop, using lambda expressions and streams but have failed.

for(int i = 0, j = 308; i < 17;i++, j -= 18) {
            if(consoleMessages[i] != null) {
                newBoldFont.drawBasicString(consoleMessages[i], 9, j, 16777215, 0);
            }
        }

I have tried this, also have tried many other ways but can't remember what I had.

Arrays.stream(consoleMessages).forEach(x->{
            IntStream.range(308,0).map(inty->inty-=18).forEach(v->{
                newBoldFont.drawBasicString(x, 9, v, 16777215, 0);
                return;
            });
        });

If you insist...

    IntStream.range(0, 17)
        .forEach(
            i-> {
                if(consoleMessages[i] != null)
                    newBoldFont.drawBasicString(consoleMessages[i], 9, 308 - 18 * i, 16777215, 0);
            });

... but WHY would anyone want to do this? The loop is perfect, it is readable, it is understandable. Leave the code as it is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM