Special enhancements on maps

Just as most methods for lists in Gosu are defined as part of Java’s class java.util.ArrayList, most of the behavior of maps in Gosu is inherited from java.util.Map. Gosu provides additional enhancements to extend maps with additional features, some of which use Gosu blocks.

Map properties for keys and values

Enhancements to the Map class add two new read-only properties:

  • keys – Calculates and returns the set of keys in the Map. This property is a wrapper for the keySet method.
  • values – Returns the values of the Map.

Each key and value

Enhancements to the Map class add the eachKeyAndValue method, which takes a block that has two arguments: one of the key type and one of the value type. This method calls this block with each key/value pair in the Map, providing a more natural iteration over the Map.

For example:

var strMap = new HashMap<String, String>(){"aKey" -> "aValue", "bKey" -> "bValue"}
strMap.eachKeyAndValue( \ key, value -> print("key : " + key + ", value : " + value ) )