groovy Collection Operators Remove duplicates


def lst = ['foo', 'foo', 'bar', 'baz']

// *modifies* the list removing duplicate items
lst.unique() // [foo, bar, baz]

// setting to false the "mutate" argument returns a new list, leaving the original intact
lst.unique(false) // [foo, bar, baz]

// convert the list to a Set, thus removing duplicates
lst.toSet() // [baz, bar, foo]

// defining a custom equality criteria. For example: to elements are equal if have the same first letter
println lst.unique() { it[0] } // [foo, bar]. 'bar' and 'baz' considered equal