Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - Map eachWithIndex(Closure closure) method



Description

Groovy Map eachWithIndex(Closure closure) method allows a map to be iterated through using passed closure while closure accepting index of each entry as additional parameter along with entry of the Map.

Syntax

public Map each(Closure closure)

Parameters

closure − 2 or 3 args closure to be applied on each entry of the map.

Return Value

Returns self.

Example - Iterating entries of a Map of String and String

Following is an example of the usage of this method −

main.groovy

// define a map
def map = ["A" : "Apple", "B" : "Banana", "C": "Carrot"] 

def result = ""
// iterate entries of map using 2 args constructor
// and update result
map.eachWithIndex{entry,index -> result += "$index($entry)"}

println(result)

result = ""

// iterate entries of map using 3 args constructors
// and update result
map.eachWithIndex{key, value, index -> result += "$index($key=$value)"}

println(result)

Output

When we run the above program, we will get the following result −

0(A=Apple)1(B=Banana)2(C=Carrot)
0(A=Apple)1(B=Banana)2(C=Carrot)

Example - Iterating entries of a Map of Integer and Integer

Following is an example of the usage of this method −

main.groovy

// define a map
def map = [1 : 11, 2 : 12, 3: 13] 

def result = ""
// iterate entries of map using 2 args constructor
// and update result
map.eachWithIndex{entry,index -> result += "$index($entry)"}

println(result)

result = ""

// iterate entries of map using 3 args constructors
// and update result
map.eachWithIndex{key, value, index -> result += "$index($key=$value)"}

println(result)

Output

When we run the above program, we will get the following result −

0(1=11)1(2=12)2(3=13)
0(1=11)1(2=12)2(3=13)

Example - Iterating entries of a Map of Integer and Object

Following is an example of the usage of this method −

main.groovy

// define a map
def map = [1 : new Student(1, "Julie"), 2 : new Student(2, "Robert"), 3: new Student(3,"Adam")] 

def result = ""
// iterate entries of map using 2 args constructor
// and update result
map.eachWithIndex{entry,index -> result += "$index($entry)"}

println(result)

result = ""

// iterate entries of map using 3 args constructors
// and update result
map.eachWithIndex{key, value, index -> result += "$index($key=$value)"}

println(result)

class Student{
   int rollNo
   String name

   Student(int rollNo, String name){
      this.rollNo = rollNo
      this.name = name
   }

   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name)
   }
   
   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]"
   }
}

Output

When we run the above program, we will get the following result −

0(1=[ 1, Julie ])1(2=[ 2, Robert ])2(3=[ 3, Adam ])
0(1=[ 1, Julie ])1(2=[ 2, Robert ])2(3=[ 3, Adam ])
groovy_maps.htm
Advertisements