Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - Map groupEntriesBy(Closure predicate) method



Description

Groovy Map groupEntriesBy(Closure predicate) method groups the members of a map into sub maps determined by the supplied mapping closure.

Syntax

public Map groupEntriesBy(Closure predicate)

Parameters

predicate − 1 or 2 arguments closure used for matching

Return Value

Map grouped by predicate

Example - Group matching entries in 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", "D":"Berry"] 

// group entries starting from B using one argument closure
def result = map.groupEntriesBy{entry -> entry.value.startsWith("B")}

println(result)

// group entries starting from B using two arguments closure
result = map.groupEntriesBy{key, value -> value.startsWith("B")}

println(result)

Output

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

[false:[A:Apple, C:Carrot], true:[B:Banana, D:Berry]]
[false:[A:Apple, C:Carrot], true:[B:Banana, D:Berry]]

Example - Group matching entries in a Map of Integer and Integer

Following is an example of the usage of this method −

main.groovy

// define maps
def map = [1 : 11, 2 : 12, 3: 13, 4:14]

// group even entries using one argument closure
def result = map.groupEntriesBy{entry -> entry.key % 2 == 0}

println(result)

// group even entries using two arguments closure
result = map.groupEntriesBy{key, value -> key % 2 ==0}

println(result)

Output

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

[false:[1:11, 3:13], true:[2:12, 4:14]]
[false:[1:11, 3:13], true:[2:12, 4:14]]

Example - Grouping matching entries in a Map of Integer and Object

Following is an example of the usage of this method −

main.groovy

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

// group even entries using one argument closure
def result = map.groupEntriesBy{entry -> entry.key % 2 == 0}

println(result)

// group even entries using two arguments closure
result = map.groupEntriesBy{key, value -> key % 2 ==0}

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 −

[false:[1:[ 1, Julie ], 3:[ 3, Adam ]], true:[2:[ 2, Robert ], 4:[ 4, Markus ]]]
[false:[1:[ 1, Julie ], 3:[ 3, Adam ]], true:[2:[ 2, Robert ], 4:[ 4, Markus ]]]
groovy_maps.htm
Advertisements