Get First/Last element in ArrayList

As we head into the late 2014 NCAA football and and the conference championship weekend is on our heels we thought we would use the Big Ten East and West for seed data. In this example we will show how to get the first and last element of an arraylist in groovy. The first() and last() was introduced in 1.8.7 and can be applied to any iterable. A couple comparable examples show how to get first element of iterator in java and how to get last element in collection in java.

Get first element

@Test
void get_first_element_in_list() {

    def list = ["Wisconsin", "Minnesota", "Nebraska",
                 "Iowa", "Illinois", "Northwestern",
                 "Purdue"]

    assert "Wisconsin" == list.first()
}

Get last element

@Test
void get_last_element_in_list() {

    def list = ["Ohio State", "Michigan State", "Maryland",
                "Michigan", "Penn State", "Rutgers",
                "Indiana"]

    assert "Indiana" == list.last()
}