Get longest string in list

This example will show how to find the longest or maximum length string contained within a list using groovy. The seed data below is a list of fictional rodents. The <=> operator or spaceship operator will return -1 if left is smaller 0 if == to right or 1 if greater than the right and in this instance will sort the collection by smallest to greatest. A comparable example we demonstrate how to get the maximum length string in a collection using java, java 8 and guava.

Sort string get first

void longest_string_arraylist() {

    def collection = [
            "Mickey Mouse",
            "Speedy Gonzales"]

    assertEquals "Speedy Gonzales",
            collection.sort({ a, b -> b.length() <=> a.length() })[0]