Count occurrences of key or value in map

Using a map of data populated from the Lincoln Home National Historic Site we will use map.count with a groovy closure to count the number of times a key and a value occur. A comparable example demonstrates how to count keys or values in a java hashmap.

Count Keys

@Test
void count_keys_in_map () {

    def map = [name: "Lincoln Home National Historic Site",
               address: "8th & Jackson Streets",
               state: "Illinois",
               city: "Springfield"]

    def frequency = map.count { key, value -> key.contains("it") }

    assert 1 == frequency
}

Count values

@Test
void count_values_in_map () {

    def map = [name: "Lincoln Home National Historic Site",
               address: "8th & Jackson Streets",
               state: "Illinois",
               city: "Springfield"]

    def frequency = map.count { key, value -> value.endsWith("s") }

    assert 2 == frequency
}