Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - Map groupBy(List predicates) method



Description

Groovy Map groupBy(List predicates) method groups the members of a map into sub maps determined by the supplied mapping closures.

Syntax

public Map groupBy(List predicates)

Parameters

predicates − list of 1 or 2 arguments closures used for matching

Return Value

Map grouped by each predicates

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.groupBy([{it.value.startsWith("B")}, { it.value.size() == 5 }])

println(result)

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

println(result)

Output

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

[false:[true:[A:Apple], false:[C:Carrot]], true:[false:[B:Banana], true:[D:Berry]]]
[false:[true:[A:Apple], false:[C:Carrot]], true:[false:[B:Banana], true:[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.groupBy([{it.key % 2 == 0}, {it.value %2 == 0}])

println(result)

// group even entries using two arguments closure
result = map.groupBy([{k, v -> k % 2 ==0},{k, v -> v % 2 ==0}])

println(result)

Output

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

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

Example - Grouping matching entries in a Map of String 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.groupBy([{it.key % 2 == 0},{it.value.getRollNo() % 2 == 0}])

println(result)

// group even entries using two arguments closure
result = map.groupBy([{k, v -> k % 2 ==0},{k, v -> v.getRollNo() % 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:[false:[1:[ 1, Julie ], 3:[ 3, Adam ]]], true:[true:[2:[ 2, Robert ], 4:[ 4, Markus ]]]]
[false:[false:[1:[ 1, Julie ], 3:[ 3, Adam ]]], true:[true:[2:[ 2, Robert ], 4:[ 4, Markus ]]]]
groovy_maps.htm
Advertisements