Package: java.util

## [Java] Interface Collection

-     public interface Collection

  GDK enhancements for Collection.

  displayName:
    Collection
  receiverFqcn:
    java.util.Collection

- - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `<T>` | `public boolean` | `addAll(Iterator<? extends T> items)` Adds all items from the iterator to the Collection.
    `<T>` | `public boolean` | `addAll(Iterable<? extends T> items)` Adds all items from the iterable to the Collection.
    `<T>` | `public boolean` | `addAll(T[] items)` Modifies the collection by adding all the elements in the specified array to the collection.
    `` | `public boolean` | `asBoolean()` Coerce a collection instance to a boolean value.
    `<T>` | `public Collection<T>` | `asChecked(Class<T> type)` Creates a checked view of a Collection.
    `<T>` | `public Collection<T>` | `asImmutable()` A convenience method for creating an immutable Collection.
    `<T>` | `public Collection<T>` | `asSynchronized()` Creates a synchronized view of a Collection.
    `<T>` | `public T` | `asType(Class<T> clazz)` Converts the given collection to another type.
    `<T>` | `public Collection<T>` | `asUnmodifiable()` Creates an unmodifiable view of a Collection.
    `` | `public List` | `collectNested(Closure transform)` Recursively iterates through this collection transforming each non-Collection value into a new value using the closure as a transformer.
    `<T>` | `public Collection<T>` | `each(Closure closure)` Iterates through a Collection, passing each item to the given closure.
    `<T>` | `public Collection<T>` | `eachWithIndex(Closure closure)` Iterates through a Collection, passing each item and the item's index (a counter starting at zero) to the given closure.
    `<T>` | `public T` | `find(Closure closure)` Finds the first value matching the closure condition.
    `<T>` | `public T` | `find()` Finds the first item matching the IDENTITY Closure (i.e. matching Groovy truth).
    `<T>` | `public Collection<T>` | `findAll(Closure closure)` Finds all values matching the closure condition.
    `<T>` | `public Collection<T>` | `findAll()` Finds the items matching the IDENTITY Closure (i.e. matching Groovy truth).
    `<T, E>` | `public Collection<T>` | `flatten()` Flatten a Collection.
    `` | `public List` | `getAt(String property)` Support the subscript operator for a Collection.
    `` | `public IntRange` | `getIndices()` Returns indices of the collection.
    `<T>` | `public Collection<T>` | `grep(Object filter)` Iterates over the collection of items and returns each item that matches the given filter - calling the `Object.isCase` method used by switch statements. method can be used with different kinds of filters like regular expressions, classes, ranges etc.
    `<T>` | `public Collection<T>` | `grep()` Iterates over the collection returning each element that matches using the IDENTITY Closure as a filter - effectively returning all elements which satisfy Groovy truth.
    `` | `public String` | `groovyToString()` Returns Groovy's default string representation for a Collection.
    `<T>` | `public Collection<T>` | `intersect(Collection<T> right)` Create a Collection composed of the intersection of both collections.
    `<T>` | `public Collection<T>` | `intersect(Collection<T> right, Comparator<? super T> comparator)` Create a Collection composed of the intersection of both collections.
    `` | `public boolean` | `isCase(Object switchValue)` Special 'case' implementation for collections which tests if the 'switch' operand is contained in any of the 'case' values.
    `` | `public boolean` | `isNotCase(Object switchValue)` Since: 4.0.0
    `<T>` | `public Collection<T>` | `leftShift(T value)` Overloads the left shift operator to provide an easy way to append objects to a Collection.
    `<T>` | `public Collection<T>` | `minus(Collection<?> removeMe)` Create a new Collection composed of the elements of the first Collection minus every occurrence of elements of the given Collection.
    `<T>` | `public Collection<T>` | `plus(Collection<T> right)` Create a Collection as a union of two collections.
    `<T>` | `public Collection<T>` | `plus(Iterable<T> right)` Create a Collection as a union of a Collection and an Iterable.
    `<T>` | `public Collection<T>` | `plus(T right)` Create a collection as a union of a Collection and an Object.
    `` | `public boolean` | `removeAll(Object[] items)` Modifies this collection by removing its elements that are contained within the specified object array.
    `<T>` | `public boolean` | `removeAll(Closure condition)` Modifies this collection by removing the elements that are matched according to the specified closure condition.
    `<E>` | `public boolean` | `removeElement(Object o)` Modifies this collection by removing a single instance of the specified element from this collection, if it is present.
    `` | `public boolean` | `retainAll(Object[] items)` Modifies this collection so that it retains only its elements that are contained in the specified array.
    `<T>` | `public boolean` | `retainAll(Closure condition)` Modifies this collection so that it retains only its elements that are matched according to the specified closure condition.
    `<T>` | `public Collection<Collection<T>>` | `split(Closure closure)` Splits all items into two collections based on the closure condition.
    `<T>` | `public Collection<Collection<T>>` | `split(Predicate<? super T> predicate)` Splits all items into two collections based on the predicate.
    `` | `public String` | `toListString()` Returns the string representation of the given list.
    `` | `public String` | `toListString(int maxSize)` Returns the string representation of the given list.
    `<T>` | `public Set<T>` | `toSet()` Convert a Collection to a Set.
    `<T>` | `public Collection<T>` | `union(Collection<T> right)` Create a Collection composed of the union of both collections.
    `<T>` | `public Collection<T>` | `union(Collection<T> right, Comparator<? super T> comparator)` Create a Collection composed of the union of both collections.
    `<T>` | `public Collection<T>` | `unique()` Modifies this collection to remove all duplicated items, using Groovy's default number-aware comparator.
    `<T>` | `public Collection<T>` | `unique(boolean mutate)` Remove all duplicates from a given Collection using Groovy's default number-aware comparator.
    `<T>` | `public Collection<T>` | `unique(Closure closure)` A convenience method for making a collection unique using a Closure to determine duplicate (equal) items.
    `<T>` | `public Collection<T>` | `unique(boolean mutate, Closure closure)` A convenience method for making a collection unique using a Closure to determine duplicate (equal) items.
    `<T>` | `public Collection<T>` | `unique(Comparator<? super T> comparator)` Remove all duplicates from a given Collection.
    `<T>` | `public Collection<T>` | `unique(boolean mutate, Comparator<? super T> comparator)` Remove all duplicates from a given Collection.

- - ### Method Detail

    - #### <T> public boolean addAll(Iterator<? extends T> items)

      Adds all items from the iterator to the Collection.

      Parameters:
        `items` - the items to add
      Returns:
        true if the collection changed

    - #### <T> public boolean addAll(Iterable<? extends T> items)

      Adds all items from the iterable to the Collection.

      Parameters:
        `items` - the items to add
      Returns:
        true if the collection changed

    - #### <T> public boolean addAll(T[] items)

      Modifies the collection by adding all the elements in the specified array to the collection. The behavior of this operation is undefined if the specified array is modified while the operation is in progress.

      See also `plus` or the '+' operator if wanting to produce a new collection containing additional items but while leaving the original collection unchanged.

      Parameters:
        `items` - array containing elements to be added to this collection
      Returns:
        `true` if this collection changed as a result of the call
      See Also:
        Collections.addAll
      Since:
        1.7.2

    - #### public boolean asBoolean()

      Coerce a collection instance to a boolean value. A collection is coerced to false if it's empty, and to true otherwise.

          assert [1,2].asBoolean() == true

          assert [].asBoolean() == false

      Returns:
        the boolean value
      Since:
        1.7.0

    - #### <T> public Collection<T> asChecked(Class<T> type)

      Creates a checked view of a Collection.

      See Also:
        Collections.checkedCollection
      Since:
        5.0.0

    - #### <T> public Collection<T> asImmutable()

      A convenience method for creating an immutable Collection.

      Returns:
        an unmodifiable view of a copy of the original, i.e. an effectively immutable copy
      See Also:
        asImmutable(java.util.List)
        asUnmodifiable(java.util.Collection)
      Since:
        1.5.0

    - #### <T> public Collection<T> asSynchronized()

      Creates a synchronized view of a Collection.

      Returns:
        a synchronized Collection
      See Also:
        Collections.synchronizedCollection
      Since:
        1.0

    - #### <T> public T asType(Class<T> clazz)

      Converts the given collection to another type. A default concrete type is used for List, Set, or SortedSet. If the given type has a constructor taking a collection, that is used. Otherwise, the call is deferred to Object.asType. If this collection is already of the given type, the same instance is returned.

      Parameters:
        `clazz` - the desired class
      Returns:
        the object resulting from this type conversion
      See Also:
        asType(java.lang.Object, java.lang.Class)
      Since:
        1.0

    - #### <T> public Collection<T> asUnmodifiable()

      Creates an unmodifiable view of a Collection.

      Returns:
        an unmodifiable view of the Collection
      See Also:
        Collections.unmodifiableCollection
        asUnmodifiable(java.util.List)
      Since:
        2.5.0

    - #### public List collectNested(Closure transform)

      Recursively iterates through this collection transforming each non-Collection value into a new value using the closure as a transformer. Returns a potentially nested list of transformed values.

           assert [2,[4,6],[8],[]] == [1,[2,3],[4],[]].collectNested { it * 2 }

      Parameters:
        `transform` - the closure used to transform each item of the collection
      Returns:
        the resultant collection
      Since:
        1.8.1

    - #### <T> public Collection<T> each(Closure closure)

      Iterates through a Collection, passing each item to the given closure.

      Parameters:
        `closure` - the closure applied on each element found
      Returns:
        the self Collection
      Since:
        2.4.0

    - #### <T> public Collection<T> eachWithIndex(Closure closure)

      Iterates through a Collection, passing each item and the item's index (a counter starting at zero) to the given closure.

      Parameters:
        `closure` - a Closure to operate on each item
      Returns:
        the self Collection
      Since:
        2.4.0

    - #### <T> public T find(Closure closure)

      Finds the first value matching the closure condition. Example:

          def list = [1,2,3]
           assert 2 == list.find { it > 1 }

      Parameters:
        `closure` - a closure condition
      Returns:
        the first Object found, in the order of the collections iterator, or null if no element matches
      Since:
        1.0

    - #### <T> public T find()

      Finds the first item matching the IDENTITY Closure (i.e. matching Groovy truth).

      Example:

           def items = [null, 0, 0.0, false, '', [], 42, 43]
           assert items.find() == 42

      Returns:
        the first Object found or null if none was found
      Since:
        1.8.1
      See Also:
        Closure.IDENTITY

    - #### <T> public Collection<T> findAll(Closure closure)

      Finds all values matching the closure condition.

          assert [2,4] == [1,2,3,4].findAll { it % 2 == 0 }

      Parameters:
        `closure` - a closure condition
      Returns:
        a Collection of matching values
      Since:
        1.5.6

    - #### <T> public Collection<T> findAll()

      Finds the items matching the IDENTITY Closure (i.e. matching Groovy truth).

      Example:

           def items = [1, 2, 0, false, true, '', 'foo', [], [4, 5], null]
           assert items.findAll() == [1, 2, true, 'foo', [4, 5]]

      Returns:
        a Collection of the truthy values
      Since:
        1.8.1
      See Also:
        Closure.IDENTITY

    - #### <T, E> public Collection<T> flatten()

      Flatten a Collection. This Collection and any nested arrays or collections have their contents (recursively) added to the new collection.

          assert [1,2,3,4,5] == [1,[2,3],[[4]],[],5].flatten()

      Returns:
        a flattened Collection
      Since:
        1.6.0

    - #### public List getAt(String property)

      Support the subscript operator for a Collection.

           assert [String, Long, Integer] == ["a",5L,2]["class"]

      Parameters:
        `property` - a String
      Returns:
        a List
      Since:
        1.0

    - #### public IntRange getIndices()

      Returns indices of the collection.

      Example:

           assert 0..2 == [5, 6, 7].indices

      Returns:
        an index range
      Since:
        2.4.0

    - #### <T> public Collection<T> grep(Object filter)

      Iterates over the collection of items and returns each item that matches the given filter - calling the `Object.isCase` method used by switch statements. method can be used with different kinds of filters like regular expressions, classes, ranges etc. Example:

           def list = ['a', 'b', 'aa', 'bc', 3, 4.5]
           assert list.grep( ~/a+/ )  == ['a', 'aa']
           assert list.grep( ~/../ )  == ['aa', 'bc']
           assert list.grep( Number ) == [ 3, 4.5 ]
           assert list.grep{ it.toString().size() == 1 } == [ 'a', 'b', 3 ]

      Parameters:
        `filter` - the filter to perform on each element of the collection (using the Object.isCase method)
      Returns:
        a collection of objects which match the filter
      Since:
        2.0

    - #### <T> public Collection<T> grep()

      Iterates over the collection returning each element that matches using the IDENTITY Closure as a filter - effectively returning all elements which satisfy Groovy truth.

      Example:

           def items = [1, 2, 0, false, true, '', 'foo', [], [4, 5], null]
           assert items.grep() == [1, 2, true, 'foo', [4, 5]]

      Returns:
        a collection of elements satisfy Groovy truth
      See Also:
        Closure.IDENTITY
      Since:
        2.0

    - #### public String groovyToString()

      Returns Groovy's default string representation for a Collection. This is used by Groovy's formatting infrastructure (e.g., GString interpolation, `println`, assert messages). By default, it delegates to FormatHelper#toListString(Collection)#toListString(Collection).

           assert [1, 2, 3].groovyToString() == '[1, 2, 3]'

      If disabled, e.g. via `-Dgroovy.extension.disable=groovyToString(Collection)`, the normal list `toString()` is used instead. Alternatively, you have the option to provide a replacement extension method in an extension module to customize how collections are displayed throughout Groovy.

      Returns:
        the string representation
      Since:
        6.0.0

    - #### <T> public Collection<T> intersect(Collection<T> right)

      Create a Collection composed of the intersection of both collections. Any elements that exist in both collections are added to the resultant collection. For collections of custom objects; the objects should implement java.lang.Comparable

          assert [4,5] == [1,2,3,4,5].intersect([4,5,6,7,8])

      By default, Groovy uses a NumberAwareComparator when determining if an element exists in both collections.
      Parameters:
        `right` - a Collection
      Returns:
        a Collection as an intersection of both collections
      See Also:
        intersect(Collection, Collection, Comparator)
      Since:
        1.5.6

    - #### <T> public Collection<T> intersect(Collection<T> right, Comparator<? super T> comparator)

      Create a Collection composed of the intersection of both collections. Any elements that exist in both collections are added to the resultant collection. For collections of custom objects; the objects should implement java.lang.Comparable

           assert [3,4] == [1,2,3,4].intersect([3,4,5,6], Comparator.naturalOrder())
           assert [2,4] == [1,2,3,4].intersect([4,8,12,16,20], (x, y) -> x * x <=> y)

           def one = ['a', 'B', 'c', 'd']
           def two = ['b', 'C', 'd', 'e']
           def compareIgnoreCase = { a, b -> a.toLowerCase() <=> b.toLowerCase() }
           assert one.intersect(two) == ['d']
           assert two.intersect(one) == ['d']
           assert one.intersect(two, compareIgnoreCase) == ['B', 'c', 'd']
           assert two.intersect(one, compareIgnoreCase) == ['b', 'C', 'd']

      Parameters:
        `right` - a Collection
        `comparator` - a Comparator
      Returns:
        a Collection as an intersection of both collections
      Since:
        2.5.0

    - #### public boolean isCase(Object switchValue)

      Special 'case' implementation for collections which tests if the 'switch' operand is contained in any of the 'case' values. For example:

          switch( 3 ) {
             case [1,3,5]:
               assert true
               break
             default:
               assert false
           }

      Parameters:
        `switchValue` - the switch value
      Returns:
        true if the caseValue is deemed to contain the switchValue
      See Also:
        Collection.contains
      Since:
        1.0

    - #### public boolean isNotCase(Object switchValue)

      Since:
        4.0.0

    - #### <T> public Collection<T> leftShift(T value)

      Overloads the left shift operator to provide an easy way to append objects to a Collection.

          def list = [1,2]
           list << 3
           assert list == [1,2,3]

      Parameters:
        `value` - an Object to be added to the collection.
      Returns:
        same collection, after the value was added to it.
      Since:
        1.0

    - #### <T> public Collection<T> minus(Collection<?> removeMe)

      Create a new Collection composed of the elements of the first Collection minus every occurrence of elements of the given Collection.

          assert [1, "a", true, true, false, 5.3] - [true, 5.3] == [1, "a", false]

      Parameters:
        `removeMe` - the elements to exclude
      Returns:
        a new Collection
      Since:
        2.4.0

    - #### <T> public Collection<T> plus(Collection<T> right)

      Create a Collection as a union of two collections. If the left collection is a Set, then the returned collection will be a Set otherwise a List. This operation will always create a new object for the result, while the operands remain unchanged.

          assert [1,2,3,4] == [1,2] + [3,4]

      Parameters:
        `right` - the right Collection
      Returns:
        the merged Collection
      Since:
        1.5.0

    - #### <T> public Collection<T> plus(Iterable<T> right)

      Create a Collection as a union of a Collection and an Iterable. If the left collection is a Set, then the returned collection will be a Set otherwise a List. This operation will always create a new object for the result, while the operands remain unchanged.

      Parameters:
        `right` - the right Iterable
      Returns:
        the merged Collection
      Since:
        1.8.7
      See Also:
        plus(Collection, Collection)

    - #### <T> public Collection<T> plus(T right)

      Create a collection as a union of a Collection and an Object. If the collection is a Set, then the returned collection will be a Set otherwise a List. This operation will always create a new object for the result, while the operands remain unchanged.

          assert [1,2,3] == [1,2] + 3

      Parameters:
        `right` - an object to add/append
      Returns:
        the resulting Collection
      Since:
        1.5.0

    - #### public boolean removeAll(Object[] items)

      Modifies this collection by removing its elements that are contained within the specified object array. See also `findAll` and `grep` when wanting to produce a new list containing items which don't match some criteria while leaving the original collection unchanged.

      Parameters:
        `items` - array containing elements to be removed from this collection
      Returns:
        `true` if this collection changed as a result of the call
      See Also:
        Collection.removeAll
      Since:
        1.7.2

    - #### <T> public boolean removeAll(Closure condition)

      Modifies this collection by removing the elements that are matched according to the specified closure condition.

          def list = ['a', 'b']
           list.removeAll { it == 'b' }
           assert list == ['a']

      See also `findAll` and `grep` when wanting to produce a new list containing items which match some criteria but leaving the original collection unchanged.
      Parameters:
        `condition` - a closure condition
      Returns:
        `true` if this collection changed as a result of the call
      See Also:
        Iterator.remove
      Since:
        1.7.2

    - #### <E> public boolean removeElement(Object o)

      Modifies this collection by removing a single instance of the specified element from this collection, if it is present. Essentially an alias for Collection.remove but with no ambiguity for Collection<Integer>.

      Example:

           def list = [1, 2, 3, 2]
           list.removeElement(2)
           assert [1, 3, 2] == list

      Parameters:
        `o` - element to be removed from this collection, if present
      Returns:
        true if an element was removed as a result of this call
      Since:
        2.4.0

    - #### public boolean retainAll(Object[] items)

      Modifies this collection so that it retains only its elements that are contained in the specified array. In other words, removes from this collection all of its elements that are not contained in the specified array. See also `grep` and `findAll` when wanting to produce a new list containing items which match some specified items but leaving the original collection unchanged.

      Parameters:
        `items` - array containing elements to be retained from this collection
      Returns:
        `true` if this collection changed as a result of the call
      See Also:
        Collection.retainAll
      Since:
        1.7.2

    - #### <T> public boolean retainAll(Closure condition)

      Modifies this collection so that it retains only its elements that are matched according to the specified closure condition. In other words, removes from this collection all of its elements that don't match.

          def list = ['a', 'b']
           list.retainAll { it == 'b' }
           assert list == ['b']

      See also `findAll` and `grep` when wanting to produce a new list containing items which match some criteria but leaving the original collection unchanged.
      Parameters:
        `condition` - a closure condition
      Returns:
        `true` if this collection changed as a result of the call
      See Also:
        Iterator.remove
      Since:
        1.7.2

    - #### <T> public Collection<Collection<T>> split(Closure closure)

      Splits all items into two collections based on the closure condition. The first list contains all items which match the closure expression. The second list all those that don't.

      Example usage:

          assert [[2,4],[1,3]] == [1,2,3,4].split { it % 2 == 0 }

      Parameters:
        `closure` - a closure condition
      Returns:
        a List whose first item is the accepted values and whose second item is the rejected values
      Since:
        1.6.0

    - #### <T> public Collection<Collection<T>> split(Predicate<? super T> predicate)

      Splits all items into two collections based on the predicate. A "fat-free" variant of Collection.split accepting a Predicate.

           assert [[2, 4], [1, 3]] == [1, 2, 3, 4].split(n -> n % 2 == 0)

      Parameters:
        `predicate` - a predicate used to determine the target collection
      Returns:
        a List whose first item is the accepted values and whose second item is the rejected values
      Since:
        6.0.0

    - #### public String toListString()

      Returns the string representation of the given list. The string displays the contents of the list, similar to a list literal, i.e. `[1, 2, a]`.

      Returns:
        the string representation
      Since:
        1.0

    - #### public String toListString(int maxSize)

      Returns the string representation of the given list. The string displays the contents of the list, similar to a list literal, i.e. `[1, 2, a]`.

      Parameters:
        `maxSize` - stop after approximately this many characters and append '...'
      Returns:
        the string representation
      Since:
        1.7.3

    - #### <T> public Set<T> toSet()

      Convert a Collection to a Set. Always returns a new Set even if the Collection is already a Set.

      Example usage:

           def result = [1, 2, 2, 2, 3].toSet()
           assert result instanceof Set
           assert result == [1, 2, 3] as Set

      Returns:
        a Set
      Since:
        1.8.0

    - #### <T> public Collection<T> union(Collection<T> right)

      Create a Collection composed of the union of both collections. Any elements that exist in either collections are added to the resultant collection, such that no elements are duplicated in the resultant collection. For collections of custom objects; the objects should implement java.lang.Comparable

          assert [1,2,3,4,5,6,7,8] == [1,2,3,4,5].union([4,5,6,7,8])

      By default, Groovy uses a NumberAwareComparator when determining if an element exists in the resultant collection.
      Parameters:
        `right` - a Collection
      Returns:
        a Collection as a union of both collections
      See Also:
        union(Collection, Collection, Comparator)
      Since:
        5.0.0

    - #### <T> public Collection<T> union(Collection<T> right, Comparator<? super T> comparator)

      Create a Collection composed of the union of both collections. Any elements that exist in either collections are added to the resultant collection, such that no elements are duplicated in the resultant collection. For collections of custom objects; the objects should implement java.lang.Comparable

           assert [1,2,3,4,5,6] == [1,2,3,4].union([3,4,5,6], Comparator.naturalOrder())
           assert [4,8,12,16,20,1,3] == [4,8,12,16,20].union([1,2,3,4], (x, y) -> x * x <=> y)

           def one = ['a', 'B', 'c', 'd']
           def two = ['b', 'C', 'd', 'e']
           def compareIgnoreCase = { a, b -> a.toLowerCase() <=> b.toLowerCase() }
           assert one.union(two) == ['a', 'B', 'c', 'd', 'b', 'C', 'e']
           assert two.union(one) == ['b', 'C', 'd', 'e', 'a', 'B', 'c']
           assert one.union(two, compareIgnoreCase) == ['a', 'B', 'c', 'd', 'e']
           assert two.union(one, compareIgnoreCase) == ['b', 'C', 'd', 'e', 'a']

      Parameters:
        `right` - a Collection
        `comparator` - a Comparator
      Returns:
        a Collection as a union of both collections
      Since:
        5.0.0

    - #### <T> public Collection<T> unique()

      Modifies this collection to remove all duplicated items, using Groovy's default number-aware comparator.

          assert [1,3] == [1,3,3].unique()

      Returns:
        the now modified collection
      See Also:
        unique(Collection, boolean)
      Since:
        1.0

    - #### <T> public Collection<T> unique(boolean mutate)

      Remove all duplicates from a given Collection using Groovy's default number-aware comparator. If mutate is true, it works by modifying the original object (and also returning it). If mutate is false, a new collection is returned leaving the original unchanged.

           assert [1,3] == [1,3,3].unique()

           def orig = [1, 3, 2, 3]
           def uniq = orig.unique(false)
           assert orig == [1, 3, 2, 3]
           assert uniq == [1, 3, 2]

      Parameters:
        `mutate` - false will return a new collection containing the unique items from the collection, true will mutate collections in place and return the original collection
      Returns:
        a collection with unique elements
      Since:
        1.8.1

    - #### <T> public Collection<T> unique(Closure closure)

      A convenience method for making a collection unique using a Closure to determine duplicate (equal) items.

      If the closure takes a single parameter, the argument passed will be each element, and the closure should return a value used for comparison (either using Comparable.compareTo or Object.equals). If the closure takes two parameters, two items from the collection will be passed as arguments, and the closure should return an int value (with 0 indicating the items are not unique).

          assert [1,4] == [1,3,4,5].unique { it % 2 }

          assert [2,3,4] == [2,3,3,4].unique { a, b -> a <=> b }

      Parameters:
        `closure` - a 1 or 2 arg Closure used to determine unique items
      Returns:
        self without any duplicates
      See Also:
        unique(Collection, boolean, Closure)
      Since:
        1.0

    - #### <T> public Collection<T> unique(boolean mutate, Closure closure)

      A convenience method for making a collection unique using a Closure to determine duplicate (equal) items. If mutate is true, it works on the receiver object and returns it. If mutate is false, a new collection is returned.

      If the closure takes a single parameter, each element from the Collection will be passed to the closure. The closure should return a value used for comparison (either using Comparable.compareTo or Object.equals). If the closure takes two parameters, two items from the collection will be passed as arguments, and the closure should return an int value (with 0 indicating the items are not unique).

           def orig = [1, 3, 4, 5]
           def uniq = orig.unique(false) { it % 2 }
           assert orig == [1, 3, 4, 5]
           assert uniq == [1, 4]

           def orig = [2, 3, 3, 4]
           def uniq = orig.unique(false) { a, b -> a <=> b }
           assert orig == [2, 3, 3, 4]
           assert uniq == [2, 3, 4]

      Parameters:
        `mutate` - false will always cause a new list to be created, true will mutate lists in place
        `closure` - a 1 or 2 arg Closure used to determine unique items
      Returns:
        self without any duplicates
      Since:
        1.8.1

    - #### <T> public Collection<T> unique(Comparator<? super T> comparator)

      Remove all duplicates from a given Collection. Works on the original object (and also returns it). The order of members in the Collection are compared by the given Comparator. For each duplicate, the first member which is returned by the given Collection's iterator is retained, but all other ones are removed. The given Collection's original order is preserved.

           class Person {
               def fname, lname
               String toString() {
                   return fname + " " + lname
               }
           }

           class PersonComparator implements Comparator {
               int compare(Object o1, Object o2) {
                   Person p1 = (Person) o1
                   Person p2 = (Person) o2
                   if (p1.lname != p2.lname)
                       return p1.lname.compareTo(p2.lname)
                   else
                       return p1.fname.compareTo(p2.fname)
               }

               boolean equals(Object obj) {
                   return this.equals(obj)
               }
           }

           Person a = new Person(fname:"John", lname:"Taylor")
           Person b = new Person(fname:"Clark", lname:"Taylor")
           Person c = new Person(fname:"Tom", lname:"Cruz")
           Person d = new Person(fname:"Clark", lname:"Taylor")

           def list = [a, b, c, d]
           List list2 = list.unique(new PersonComparator())
           assert( list2 == list && list == [a, b, c] )

      Parameters:
        `comparator` - a Comparator
      Returns:
        self the now modified collection without duplicates
      See Also:
        unique(java.util.Collection, boolean, java.util.Comparator)
      Since:
        1.0

    - #### <T> public Collection<T> unique(boolean mutate, Comparator<? super T> comparator)

      Remove all duplicates from a given Collection. If mutate is true, it works on the original object (and also returns it). If mutate is false, a new collection is returned. The order of members in the Collection are compared by the given Comparator. For each duplicate, the first member which is returned by the given Collection's iterator is retained, but all other ones are removed. The given Collection's original order is preserved.

           class Person {
               def fname, lname
               String toString() {
                   return fname + " " + lname
               }
           }

           class PersonComparator implements Comparator {
               int compare(Object o1, Object o2) {
                   Person p1 = (Person) o1
                   Person p2 = (Person) o2
                   if (p1.lname != p2.lname)
                       return p1.lname.compareTo(p2.lname)
                   else
                       return p1.fname.compareTo(p2.fname)
               }

               boolean equals(Object obj) {
                   return this.equals(obj)
               }
           }

           Person a = new Person(fname:"John", lname:"Taylor")
           Person b = new Person(fname:"Clark", lname:"Taylor")
           Person c = new Person(fname:"Tom", lname:"Cruz")
           Person d = new Person(fname:"Clark", lname:"Taylor")

           def list = [a, b, c, d]
           List list2 = list.unique(false, new PersonComparator())
           assert( list2 != list && list2 == [a, b, c] )

      Parameters:
        `mutate` - false will always cause a new collection to be created, true will mutate collections in place
        `comparator` - a Comparator
      Returns:
        self the collection without duplicates
      Since:
        1.8.1