This example will show how to transform a list to a map using groovy. In the set up we will create a Bike class while initializing a list of bikes which will be used in the first snippet. In a comparable example we demonstrate how to convert an arraylist to a map in java using java 8 and guava.
Convert list of objects to map
Using collectEntries we will iterate over the collection of bikes and transform each bike object in the list to a map entry using the bike name as the key and the brand as the value.
List of strings to map
Using a SpreadMap we will convert a list of strings into a map. A SpreadMap is a helper that turns a list with an even number of elements into a Map. In the snippet below, we create a map keyed by NFL city while the value will be the team name.