Combine two lists

This example will show how to join two lists in groovy while using the addAll. The collection addAll method will add all of the elements in first collection to the second collection. It returns true if the collection has been modified although not shown below. A similar example showing how combine two arraylists in java can be used for reference when working with guava and apache commons.

Combine lists with addAll

@Test
public void combine_two_lists () {

    def first = ["a", "b", "c"]
    def second = ["d", "e", "f"]

    assert ["a", "b", "c", "d", "e", "f"], first.addAll(second)
}

Combine lists with plus operator

@Test
public void combine_two_lists_plus () {

    def first = ["a", "b", "c"]
    def second = ["d", "e", "f"]

    assert ["a", "b", "c", "d", "e", "f"] == (first + second)

}