Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "continue" in groovy's each loop

I am new to groovy (worked on java), trying to write some test cases using Spock framework. I need the following Java snippet converted into groovy snippet using "each loop"

Java Snippet:

List<String> myList = Arrays.asList("Hello", "World!", "How", "Are", "You");
for( String myObj : myList){
    if(myObj==null) {
        continue;   // need to convert this part in groovy using each loop
    }
    System.out.println("My Object is "+ myObj);
}

Groovy Snippet:

def myObj = ["Hello", "World!", "How", "Are", "You"]
myList.each{ myObj->
    if(myObj==null){
        //here I need to continue
    }
    println("My Object is " + myObj)
}
like image 718
Karthikeyan Avatar asked Jan 04 '17 14:01

Karthikeyan


People also ask

How do you continue groovy?

Groovy - Continue Statement The continue statement complements the break statement. Its use is restricted to while and for loops. When a continue statement is executed, control is immediately passed to the test condition of the nearest enclosing loop to determine whether the loop should continue.

How do you break groovy on each loop?

Nope, you can't abort an "each" without throwing an exception. You likely want a classic loop if you want the break to abort under a particular condition. Alternatively, you could use a "find" closure instead of an each and return true when you would have done a break.

How do you write a loop in groovy?

Groovy Programming Fundamentals for Java Developers The for-in statement is generally used in the following way. for(variable in range) { statement #1 statement #2 … } The following diagram shows the diagrammatic explanation of this loop. The for-in statement can also be used to loop through ranges.


3 Answers

Either use return, as the closure basically is a method that is called with each element as parameter like

def myObj = ["Hello", "World!", "How", "Are", "You"]
myList.each{ myObj->
    if(myObj==null){
        return
    }
    println("My Object is " + myObj)
}

Or switch your pattern to

def myObj = ["Hello", "World!", "How", "Are", "You"]
myList.each{ myObj->
    if(myObj!=null){
        println("My Object is " + myObj)
    }
}

Or use a findAll before to filter out null objects

def myList = ["Hello", "World!", "How", "Are", null, "You"]
myList.findAll { it != null }.each{ myObj->
    println("My Object is " + myObj)
}
like image 156
Vampire Avatar answered Oct 26 '22 03:10

Vampire


you can either use a standard for loop with continue:

for( String myObj in myList ){
  if( something ) continue
  doTheRest()
}

or use return in each's closure:

myList.each{ myObj->
  if( something ) return
  doTheRest()
}
like image 29
injecteer Avatar answered Oct 26 '22 05:10

injecteer


You could also only enter your if statement if the object isn't null.

def myObj = ["Hello", "World!", "How", "Are", "You"]
myList.each{ 
    myObj->
    if(myObj!=null){
        println("My Object is " + myObj)
    }
}
like image 1
Andrew_CS Avatar answered Oct 26 '22 03:10

Andrew_CS