Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - Map values() method



Description

Groovy Map values() method obtain a collection of the values in this Map.

Syntax

public Collection values()

Parameters

NA

Return Value

Collection of values.

Example - Getting Collection of values from a Map of String and String

Following is an example of the usage of this method −

main.groovy

// define a map
def map = ["TopicName" : "Maps", "TopicDescription" : "Methods in Maps"] 

// get the collection of values
println(map.values())

Output

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

[Maps, Methods in Maps]

Example - Getting Collection of values from a Map of Integer and String

Following is an example of the usage of this method −

main.groovy

// define a map
def map = [1 : "Apple", 2 : "Banana"] 

// get the collection of values
println(map.values())

Output

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

[Apple, Banana]

Example - Getting Collection of values from 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")] 

// get the collection of values
println(map.values())

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 −

[[ 1, Julie ], [ 2, Robert ]]
groovy_maps.htm
Advertisements