-2

Could you please give me a tip, how do I can to create lambda expression for next Java code:

for (String symbol : word) {
  sendKeys(symbol);

  if (isElementVisible(word)) {
    clickByVisibleText(word);
    break;
  }
}

I understand how to implement loop cycle,with if expression, but I don't understand how can I do that if loop contains some methods(like sendKeys()) and if with break operator.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
torori
  • 25
  • 2
  • 2
    I'm having a hard time trying to understand your question... what do you mean by "create lambda expression for next Java code"? – Luigi Cortese Sep 13 '15 at 15:14
  • 1
    Are you sure you mean a lambda expression and not a `Stream` pipeline? – RealSkeptic Sep 13 '15 at 15:16
  • 2
    Possibly related: [Limit a stream by a predicate](http://stackoverflow.com/questions/20746429/limit-a-stream-by-a-predicate) – Pshemo Sep 13 '15 at 15:18
  • 4
    That code doesn't make much sense. Inside a loop on each `symbol` in `word`, you test `isElementVisible` on **`word`** rather than `symbol`. If it's true, it's true right at the first iteration. If it's false, it will be false for all. It shouldn't be inside the loop at all. – RealSkeptic Sep 13 '15 at 15:34
  • What do you want to reach? Where do you want to use your lambda expression (if it is lambda expression what you really mean)? – Honza Zidek Sep 14 '15 at 13:18

1 Answers1

0

I suppose that what you mean is to change the external iteration over your collection into internal iteration using lambda expression.

You are probably struggling with the need to finish abruptly the internal forEach() loop.

According to the documentation for Iterable.forEach():

Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception... Exceptions thrown by the action are relayed to the caller.

So instead of using break, you throw an exception which will immediately break the internal loop.

The code will be something like this - I cannot say I like it but it works. You create your own class BreakException which extends RuntimeException.

try {
    word.forEach(symbol -> {
        sendKeys(symbol);
        if (isElementVisible(word)) { // see N.B.
            clickByVisibleText(word);
            throw new BreakException();
        }
    });
}
catch (BreakException e) {
        // here you know that your condition has been fulfilled at least once
}

Notice that the try...catch is not around the lambda expression, but rather around the whole forEach() method. To make it more visible, see the following transcription of the code which shows it more clearly:

Consumer<? super String> findElement = symbol -> {
    sendKeys(symbol);
    if (isElementVisible(word)) { // see N.B.
        clickByVisibleText(word);
        throw new BreakException();
    }
});

try {
    word.forEach(findElement);
}
catch (BreakException e) {
    // here you know that your condition has been fulfilled at least once
}

N.B.: Please re-consider also your code

if (isElementVisible(word)) {
    clickByVisibleText(word);
    throw new BreakException();
}

It looks quite suspicious. Here in the middle of the loop you keep referring to the whole collection word which stinks.

Honza Zidek
  • 9,204
  • 4
  • 72
  • 118