

Supplied mapping closure and counts the frequency of the created groups.ĭrops the given number of key/value pairs from the head of this map if they are available.Ĭreate a suffix of the given Map by dropping as many entries as possible from theįront of the original Map such that calling the given closure condition evaluates to Groups the members of a map into groups determined by the Projects each item from a source map to a result collection and concatenates (flattens) the resultingĬollections adding them into a collection.ĬollectMany( Object collector, Closure projection)Ĭollections adding them into the collector.Ĭounts the number of occurrences which satisfy the given closure from inside this map. Returning a map of the transformed entries. Iterates through this Map transforming each map entry using the transform closure Iterates through this Map transforming each entry using the transform closureĪnd returning a map of the transformed entries.ĬollectEntries( Map collector, Closure transform) Returning the collector with all transformed values added to it. Iterates through this Map transforming each map entry into a new value using the transform closureĬollect( Object collector, Closure transform) Method names, and values as the implementation. over the entries of a map, and checks whether a predicate isĬoerce a map instance to a boolean value.Ī convenience method for creating an immutable Map.Ī convenience method for creating a synchronized Map.Ĭoerces this map to the given type, using the map's keys as the public The call returns the previous value mapped to the key. If the key is not mapped to any value, replacement is not done. In other words, map.containsKey(key) must be true for the replacement operation to take place. It will replace the entry for the specified key only if it is currently mapped to some value.

The first version of the replace method accepts a key and a value. There are two versions of the replace method. The final map has entries for keys 2 and 3 as shown. The second call asks the map to remove the entry for the key 2 if it is mapped to the value “value”. Since it is false, the entry will not be removed. The first call will remove the entry with key 1 as it is mapped to value “a”. The operation returns a boolean indicating if there was a removal or not. In other words, it accepts a key and a value and removes the entry for the key only if it is currently mapped to the passed value. It removes an entry for the specified key only if it is currently mapped to the specified value. The remove method allows us to do conditional removal. As an example, we can use this to print the map in a custom format. The default implementation passes each entry (key and the value) in the map to this BiConsumer. The forEach method accepts a BiConsumer (a functional interface accepting two values) which does some action on the passed values. Refer to the Convenience Factory Methods for Collections post to learn more about them. Note: The map was created using the static factory method of in the Map interface (which was added in Java 9). Hence, it will return the default value we passed. The last call queries the map for the key 4, which is not present in the map. The first two calls would return values a and bas keys 1 and 2 are present in the map. This is useful to avoid if checks – to check first if the key is present in the map using containsKey. It returns the value mapped to the key in the map (if present) or it returns the default value passed. We pass a key and a default value to the getOrDefault method. The following default methods were added to the Map interface in Java 8: In this post we will learn about the default methods in the Java Map interface. Many JDK interfaces were enhanced with default methods. Replace if the key is mapped to the specified value.Replace if the key is mapped to a value.
