Groovy - Map plus(String right) method
Description
Groovy Map plus(String right) method appends a String to the literal of the Map instance.
Syntax
public String plus(String right)
Parameters
right − A String
Return Value
The concatenated string.
Example - Concatenating a Gstring to 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"] // add a GString to the map def result = map + " is a map" println(result)
Output
When we run the above program, we will get the following result −
[A:Apple, B:Banana] is a map
Example - Concatenating a Gstring to a Map of Integer and Integer
Following is an example of the usage of this method −
main.groovy
// define map def map = [1 : 11, 2 : 12] // add a GString to the map def result = map + " is a map" println(result)
Output
When we run the above program, we will get the following result −
[1:11, 2:12] is a map
Example - Concatenating a Gstring to 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")]
// add a GString to the map
def result = map + " is a map"
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 −
[1:[ 1, Julie ], 2:[ 2, Robert ]] is a map
groovy_maps.htm
Advertisements