Groovy JDK

java.util
Class Map

Method Summary
boolean any(Closure closure)
Iterates over the entries of a map, and checks whether a predicate is valid for at least one entry
Map asImmutable()
A convenience method for creating an immutable map
Map asSynchronized()
A convenience method for creating a synchronized Map
Object asType(Class clazz)
Coerces this map to the given type, using the map's keys as the public method names, and values as the implementation would be a closure which behaves like the method implementation
Collection collect(Collection collection, Closure closure)
Iterates through this Map transforming each entry into a new value using the closure as a transformer, returning a list of transformed values
List collect(Closure closure)
Iterates through this Map transforming each entry into a new value using the closure as a transformer, returning a list of transformed values
Map each(Closure closure)
Allows a Map to be iterated through using a closure closure takes one parameter then it will be passed the Map otherwise if the closure takes two parameters then it will be passed the key and the value
Object eachWithIndex(Closure closure)
Allows a Map to be iterated through using a closure closure takes two parameters then it will be passed the Map the item's index (a counter starting at zero) otherwise if the closure takes three parameters then it will be passed the key, the value, and the index
boolean every(Closure closure)
Iterates over the entries of a map, and checks whether a predicate is valid for all entries
Object find(Closure closure)
Finds the first entry matching the closure condition two parameters, the entry key and value are passed one parameter, the Map
Map findAll(Closure closure)
Finds all entries matching the closure condition closure takes one parameter then it will be passed the Map Otherwise if the closure should take two parameters, which will be the key and the value
Object get(Object key, Object defaultValue)
Looks up an item in a Map for the given key and returns the value - unless there is no entry for the given key in which case add the default value to the map and return that
Object getAt(Object key)
Support the subscript operator for a Map
Map groupBy(Closure closure)
Groups the members of a map into sub maps determined by the supplied mapping closure key and value (depending on the number of parameters the closure accepts) and should return the key that each item should be grouped under resulting map will have an entry for each 'group' key returned by the closure, with values being the map members from the original map that belong to each group
Map groupEntriesBy(Closure closure)
Groups all map entries into groups determined by the supplied mapping closure key and value (depending on the number of parameters the closure accepts) and should return the key that each item should be grouped under resulting map will have an entry for each 'group' key returned by the closure, with values being the list of map entries that belong to each group
Map plus(Map right)

Returns a new Map containing all entries from left and right, giving precedence to right will appear in the resultant map with values from the right operand or Properties, the returned Map will preserve that type, otherwise a HashMap will be returned

Roughly equivalent to Map m = new HashMap(); m but with some additional logic to preserve the left Map type for common cases as described above

Object putAt(Object key, Object value)
A helper method to allow lists to work with subscript operators
SpreadMap spread()
Synonym for {@link #toSpreadMap(Map)}
Map subMap(Collection keys)
Creates a sub-Map containing the given keys List
String toMapString()
Returns the string representation of this map contents of the map, i
SpreadMap toSpreadMap()
Returns a new SpreadMap from this map

For examples, if there is defined a function like as

def fn(a, b, c, d) { return a + b + c + d }
, then all of the following three have the same meaning
println fn(a:1, [b:2, c:3]
println fn(a:1, *:[b:2, c:3], d:4)
println fn(a:1, b:2, c:3, d:4)

String toString()
Returns the string representation of the given map
 
Method Detail

any

public boolean any(Closure closure)
Iterates over the entries of a map, and checks whether a predicate is valid for at least one entry

Parameters:
closure - the closure predicate used for matching.
Returns:
true if any entry in the map matches the closure predicate

asImmutable

public Map asImmutable()
A convenience method for creating an immutable map.

Returns:
an immutable Map
See:
Collections#unmodifiableMap.

asSynchronized

public Map asSynchronized()
A convenience method for creating a synchronized Map.

Returns:
a synchronized Map
See:
Collections#synchronizedMap.

asType

public Object asType(Class clazz)
Coerces this map to the given type, using the map's keys as the public method names, and values as the implementation. Typically the value would be a closure which behaves like the method implementation.

Parameters:
clazz - the target type.
Returns:
a Proxy of the given type, which defers calls to this map's elements.

collect

public Collection collect(Collection collection, Closure closure)
Iterates through this Map transforming each entry into a new value using the closure as a transformer, returning a list of transformed values.

Parameters:
collection - the Collection to which the mapped values are added.
closure - the closure used for mapping, which can take one (Map.Entry) or two (key, value) parameters.
Returns:
a List of the mapped values

collect

public List collect(Closure closure)
Iterates through this Map transforming each entry into a new value using the closure as a transformer, returning a list of transformed values.

Parameters:
closure - the closure used to map each element of the collection.
Returns:
the resultant collection

each

public Map each(Closure closure)
Allows a Map to be iterated through using a closure. If the closure takes one parameter then it will be passed the Map.Entry otherwise if the closure takes two parameters then it will be passed the key and the value.

Parameters:
closure - the closure applied on each entry of the map.
Returns:
returns the self parameter

eachWithIndex

public Object eachWithIndex(Closure closure)
Allows a Map to be iterated through using a closure. If the closure takes two parameters then it will be passed the Map.Entry and the item's index (a counter starting at zero) otherwise if the closure takes three parameters then it will be passed the key, the value, and the index.

Parameters:
closure - a Closure to operate on each item.
Returns:
the self Object

every

public boolean every(Closure closure)
Iterates over the entries of a map, and checks whether a predicate is valid for all entries.

Parameters:
closure - the closure predicate used for matching.
Returns:
true if every entry of the map matches the closure predicate

find

public Object find(Closure closure)
Finds the first entry matching the closure condition. If the closure takes two parameters, the entry key and value are passed. If the closure takes one parameter, the Map.Entry object is passed.

Parameters:
closure - a closure condition.
Returns:
the first Object found

findAll

public Map findAll(Closure closure)
Finds all entries matching the closure condition. If the closure takes one parameter then it will be passed the Map.Entry. Otherwise if the closure should take two parameters, which will be the key and the value.

Parameters:
closure - a closure condition applying on the entries.
Returns:
a new subMap

get

public Object get(Object key, Object defaultValue)
Looks up an item in a Map for the given key and returns the value - unless there is no entry for the given key in which case add the default value to the map and return that.

Parameters:
key - the key to lookup the value of.
defaultValue there - the value to return and add to the map for this key if there is no entry for the given key.
Returns:
the value of the given key or the default value, added to the map if the key did not exist

getAt

public Object getAt(Object key)
Support the subscript operator for a Map.

Parameters:
key - an Object as a key for the map.
Returns:
the value corresponding to the given key

groupBy

public Map groupBy(Closure closure)
Groups the members of a map into sub maps determined by the supplied mapping closure. The closure will be passed a Map.Entry or key and value (depending on the number of parameters the closure accepts) and should return the key that each item should be grouped under. The resulting map will have an entry for each 'group' key returned by the closure, with values being the map members from the original map that belong to each group.

Parameters:
closure - a closure mapping entries on keys.
Returns:
a new Map grouped by keys

groupEntriesBy

public Map groupEntriesBy(Closure closure)
Groups all map entries into groups determined by the supplied mapping closure. The closure will be passed a Map.Entry or key and value (depending on the number of parameters the closure accepts) and should return the key that each item should be grouped under. The resulting map will have an entry for each 'group' key returned by the closure, with values being the list of map entries that belong to each group.

Parameters:
closure - a closure mapping entries on keys.
Returns:
a new Map grouped by keys

plus

public Map plus(Map right)

Returns a new Map containing all entries from left and right, giving precedence to right. Any keys appearing in both Maps will appear in the resultant map with values from the right operand. If the left map is one of TreeMap, LinkedHashMap, Hashtable or Properties, the returned Map will preserve that type, otherwise a HashMap will be returned.

Roughly equivalent to Map m = new HashMap(); m.putAll(left); m.putAll(right); return m; but with some additional logic to preserve the left Map type for common cases as described above.

Parameters:
right - a Map.
Returns:
a new Map containing all entries from left and right

putAt

public Object putAt(Object key, Object value)
A helper method to allow lists to work with subscript operators

Parameters:
key - an Object as a key for the map.
value - the value to put into the map.
Returns:
the value corresponding to the given key

spread

public SpreadMap spread()
Synonym for {@link #toSpreadMap(Map)}.

Returns:
a newly created Spreadmap

subMap

public Map subMap(Collection keys)
Creates a sub-Map containing the given keys. This method is similar to List.subList() but uses keys rather than index ranges.

Parameters:
keys - a Collection of keys.
Returns:
a new Map containing the given keys

toMapString

public String toMapString()
Returns the string representation of this map. The string displays the contents of the map, i.e. {one=1, two=2, three=3}.

Returns:
the string representation

toSpreadMap

public SpreadMap toSpreadMap()
Returns a new SpreadMap from this map.

For examples, if there is defined a function like as

def fn(a, b, c, d) { return a + b + c + d }
, then all of the following three have the same meaning.
println fn(a:1, [b:2, c:3].toSpreadMap(), d:4)
println fn(a:1, *:[b:2, c:3], d:4)
println fn(a:1, b:2, c:3, d:4)

Returns:
a newly created Spreadmap if this list is not null and its size is positive.
See:
SpreadMap#SpreadMap.

toString

public String toString()
Returns the string representation of the given map.

Returns:
the string representation
See:
#toMapString(Map).

Groovy JDK