Overview | Package | Class | Tree | Deprecated | Index | Help
| Groovy JDK

## java.util
Class Collection

Method Summary
`Collection` | `asImmutable()` A convenience method for creating an immutable Collection
`List` | `asList()` Converts this collection to a List
`Collection` | `asSynchronized()` A convenience method for creating a synchronized Collection
`Object` | `asType(Class clazz)` Converts the given collection to another type type is used for List, Set, or SortedSet a constructor taking a collection, that is used call is deferred to {link #asType(Object,Class)} collection is already of the given type, the same instance is returned
`List` | `collect(Closure closure)` Iterates through this collection transforming each entry into a new value using the closure as a transformer, returning a list of transformed values
`Collection` | `collect(Collection collection, Closure closure)` Iterates through this collection transforming each value into a new value using the closure as a transformer, returning an initial collection plus the transformed values
`List` | `collectAll(Closure closure)` Recursively iterates through this collection transforming each non-Collection value into a new value using the closure as a transformer list of transformed values
`Collection` | `collectAll(Collection collection, Closure closure)` Recursively iterates through this collection transforming each non-Collection value into a new value using the closure as a transformer collection of transformed values
`List` | `combinations()` Adds GroovyCollections#combinations(Collection) as a method on collections
`int` | `count(Object value)` Counts the number of occurrences of the given value inside this collection Comparison is done using Groovy's == operator (using `compareTo(value) == 0` or `equals(value)` )
`boolean` | `disjoint(Collection right)` Returns `true` if the intersection of two collections is empty
`Object` | `find(Closure closure)` Finds the first value matching the closure condition
    def list = [1,2,3]
    list
`Collection` | `findAll(Closure closure)` Finds all values matching the closure condition
`Collection` | `flatten()` Flatten a collection collections have their contents (recursively) added to the new collection WARNING: Any Maps found in the collection are flattened to the Map's values and merged into the flattened collection may change in a future release use flatten(Collection, Closure) with an identity closure
`Collection` | `flatten(Closure flattenUsing)` Flatten a collection collections have their contents (recursively) added to the new collection For any non-Array, non-Collection object which represents some sort of collective type, the supplied closure should yield the contained items; otherwise, the closure should just return any element which corresponds to a leaf
`List` | `getAt(String property)` Support the subscript operator for List
`Map` | `groupBy(Closure closure)` Sorts all collection members into groups determined by the supplied mapping closure item should be grouped by distinct key returned from the closure, with each value being a list of items for that group
`Object` | `inject(Object value, Closure closure)` Iterates through the given collection, passing in the initial value to the closure along with the current iterated item then passing into the next iteration the value of the previous closure
`Collection` | `intersect(Collection right)` Create a Collection composed of the intersection of both collections elements that exist in both collections are added to the resultant collection
`boolean` | `isCase(Object switchValue)` 'Case' implementation for collections which tests if the 'switch' operand is contained in any of the 'case' values For example:
    switch( item ) {
    case firstList :
    // item is contained in this list
    // etc
    }
`String` | `join(String separator)` Concatenates the `toString()` representation of each item in this collection, with the given String as a separator between each item
`Collection` | `leftShift(Object value)` Overloads the left shift operator to provide an easy way to append objects to a Collection
`Object` | `max()` Adds max() method to Collection objects
`Object` | `max(Closure closure)` Selects the maximum value found in the collection using the given closure as a comparator number) for each item passed returns the largest comparable value will be returned from this method as the maximum
`Object` | `max(Comparator comparator)` Selects the maximum value found in the collection using the given comparator
`Object` | `min()` Adds min() method to Collection objects
`Object` | `min(Comparator comparator)` Selects the minimum value found in the collection using the given comparator
`Object` | `min(Closure closure)` Selects the minimum value found in the collection using the given closure as a comparator number) for each item passed returns the smallest comparable value will be returned from this method as the minimum
`List` | `multiply(Number factor)` Create a List composed of the elements of this list, repeated a certain number of times elements, multiple references to the same instance will be added
`Collection` | `plus(Collection right)` Create a Collection as a union of two collections 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
`Collection` | `plus(Object right)` Create a collection as a union of a Collection and an Object 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
`List` | `sort()` Sorts the given collection into a sorted list assumed to be comparable
`List` | `sort(Comparator comparator)` Sorts the Collection using the given comparator sorted into a new list, and the existing collection is unchanged
`List` | `sort(Closure closure)` Sorts this Collection using the given closure as a comparator closure is passed each item from the collection, and is assumed to return a comparable value (i
`Object` | `sum()` Sums the items in a collection "plus" method on all items in the collection
`Object` | `sum(Object initialValue)` Sums the items in a collection, adding the result to some initial value
`Object` | `sum(Closure closure)` Sums the result of apply a closure to each item of a collection `coll `coll``
`Object` | `sum(Object initialValue, Closure closure)` Sums the result of apply a closure to each item of a collection to sum intial value `coll `coll``
`List` | `toList()` Convert a collection to a List
`String` | `toListString()` Returns the string representation of the given list displays the contents of the list, similar to a list literal, i `[1, 2, a]`
`String` | `toString()` Returns the string representation of the given collection displays the contents of the collection, i `[1, 2, a]`
`List` | `transpose()` Adds GroovyCollections#transpose(Collection) as a method on collections
`Collection` | `unique()` Modifies this collection to remove all duplicated items, using the default comparator
`Collection` | `unique(Closure closure)` A convenience method for making a collection unique using a closure as a comparator argument passed will be each element, and the closure should return a value used for comparison (either using {@link Comparable#compareTo(Object)} or Object#equals() ) 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)
`Collection` | `unique(Comparator comparator)` Remove all duplicates from a given Collection Works on the receiver object and 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
    public String toString() {
    return fname + " " + lname
    }
    }

    class PersonComparator implements Comparator {
    public int compare(Object o1, Object o2) {
    Person p1 = (Person) o1
    Person p2 = (Person) o2
    if (p1
    return p1
    else
    return p1
    }

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

    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
    assert( list2 == list && list == [a, b, c] )
`

Method Detail

### asImmutable

    public Collection asImmutable()

  A convenience method for creating an immutable Collection.

Returns:
  an immutable Collection

See:
  Collections#unmodifiableCollection.

### asList

    public List asList()

  Converts this collection to a List.

Returns:
  a newly created List if this collection is not already a List

### asSynchronized

    public Collection asSynchronized()

  A convenience method for creating a synchronized Collection.

Returns:
  a synchronized Collection

See:
  Collections#synchronizedCollection.

### asType

    public Object asType(Class 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 {link #asType(Object,Class)}. 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:
  #asType(Object,Class).

### collect

    public List collect(Closure closure)

  Iterates through this collection transforming each entry into a new value using the closure as a transformer, returning a list of transformed values.

Parameters:
  `closure` - the closure used for mapping.
Returns:
  a List of the transformed values

### collect

    public Collection collect(Collection collection, Closure closure)

  Iterates through this collection transforming each value into a new value using the closure as a transformer, returning an initial collection plus the transformed values.

Parameters:
  `collection` - an initial Collection to which the transformed values are added.
  `closure` - the closure used to transform each element of the collection.
Returns:
  the resulting collection of transformed values

### collectAll

    public List collectAll(Closure closure)

  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.

Parameters:
  `closure` - the closure used to transform each element of the collection.
Returns:
  the resultant collection

### collectAll

    public Collection collectAll(Collection collection, Closure closure)

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

Parameters:
  `collection` - an initial Collection to which the transformed values are added.
  `closure` - the closure used to transform each element of the collection.
Returns:
  the resultant collection

### combinations

    public List combinations()

  Adds GroovyCollections#combinations(Collection) as a method on collections.

Returns:
  a List of the combinations found

See:
  GroovyCollections#combinations.

### count

    public int count(Object value)

  Counts the number of occurrences of the given value inside this collection. Comparison is done using Groovy's == operator (using `compareTo(value) == 0` or `equals(value)` ).

Parameters:
  `value` - the value being searched for.
Returns:
  the number of occurrences

### disjoint

    public boolean disjoint(Collection right)

  Returns `true` if the intersection of two collections is empty.

Parameters:
  `right` - a Collection.
Returns:
  boolean `true` if the intersection of two collections is empty, `false` otherwise.

### find

    public Object find(Closure closure)

  Finds the first value matching the closure condition. Example:

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

Parameters:
  `closure` - a closure condition.
Returns:
  the first Object found

### findAll

    public Collection findAll(Closure closure)

  Finds all values matching the closure condition.

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

### flatten

    public Collection flatten()

  Flatten a collection. This collection and any nested arrays or collections have their contents (recursively) added to the new collection. WARNING: Any Maps found in the collection are flattened to the Map's values and merged into the flattened collection. This behavior may change in a future release. If you don't want Maps to be flattened use flatten(Collection, Closure) with an identity closure.

Returns:
  a flattened Collection

### flatten

    public Collection flatten(Closure flattenUsing)

  Flatten a collection. This collection and any nested arrays or collections have their contents (recursively) added to the new collection. For any non-Array, non-Collection object which represents some sort of collective type, the supplied closure should yield the contained items; otherwise, the closure should just return any element which corresponds to a leaf.

Parameters:
  `flattenUsing` - a closure to determine how to flatten non-Array, non-Collection elements.
Returns:
  a flattened Collection

### getAt

    public List getAt(String property)

  Support the subscript operator for List

Parameters:
  `property` - a String.
Returns:
  a List

### groupBy

    public Map groupBy(Closure closure)

  Sorts all collection members into groups determined by the supplied mapping closure. The closure should return the key that this item should be grouped by. The returned Map will have an entry for each distinct key returned from the closure, with each value being a list of items for that group.

Parameters:
  `closure` - a closure mapping entries on keys.
Returns:
  a new Map grouped by keys

### inject

    public Object inject(Object value, Closure closure)

  Iterates through the given collection, passing in the initial value to the closure along with the current iterated item then passing into the next iteration the value of the previous closure.

Parameters:
  `value` - a value.
  `closure` - a closure.
Returns:
  the last value of the last iteration

### intersect

    public Collection intersect(Collection right)

  Create a Collection composed of the intersection of both collections. Any elements that exist in both collections are added to the resultant collection.

Parameters:
  `right` - a Collection.
Returns:
  a Collection as an intersection of both collections

### isCase

    public boolean isCase(Object switchValue)

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

      switch( item ) {
      case firstList :
      // item is contained in this list
      // etc
      }

Parameters:
  `switchValue` - the switch value.
Returns:
  true if the caseValue is deemed to contain the switchValue

See:
  Collection#contains.

### join

    public String join(String separator)

  Concatenates the `toString()` representation of each item in this collection, with the given String as a separator between each item.

Parameters:
  `separator` - a String separator.
Returns:
  the joined String

### leftShift

    public Collection leftShift(Object value)

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

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

### max

    public Object max()

  Adds max() method to Collection objects.

Returns:
  the maximum value

See:
  GroovyCollections#max.

### max

    public Object max(Closure closure)

  Selects the maximum value found in the collection using the given closure as a comparator. The closure should return a comparable value (i.e. a number) for each item passed. The collection item for which the closure returns the largest comparable value will be returned from this method as the maximum.

Parameters:
  `closure` - a closure used as a comparator.
Returns:
  the maximum value

### max

    public Object max(Comparator comparator)

  Selects the maximum value found in the collection using the given comparator.

Parameters:
  `comparator` - a Comparator.
Returns:
  the maximum value

### min

    public Object min()

  Adds min() method to Collection objects.

Returns:
  the minimum value

See:
  GroovyCollections#min.

### min

    public Object min(Comparator comparator)

  Selects the minimum value found in the collection using the given comparator.

Parameters:
  `comparator` - a Comparator.
Returns:
  the minimum value

### min

    public Object min(Closure closure)

  Selects the minimum value found in the collection using the given closure as a comparator. The closure should return a comparable value (i.e. a number) for each item passed. The collection item for which the closure returns the smallest comparable value will be returned from this method as the minimum.

Parameters:
  `closure` - a closure used as a comparator.
Returns:
  the minimum value

### multiply

    public List multiply(Number factor)

  Create a List composed of the elements of this list, repeated a certain number of times. Note that for non-primitive elements, multiple references to the same instance will be added.

Parameters:
  `factor` - the number of times to append.
Returns:
  the multiplied list

### plus

    public Collection plus(Collection 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.

Parameters:
  `right` - the right Collection.
Returns:
  the merged Collection

### plus

    public Collection plus(Object 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.

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

### sort

    public List sort()

  Sorts the given collection into a sorted list. The collection items are assumed to be comparable.

Returns:
  the sorted collection as a List

### sort

    public List sort(Comparator comparator)

  Sorts the Collection using the given comparator. The elements are sorted into a new list, and the existing collection is unchanged.

Parameters:
  `comparator` - a Comparator used for the comparison.
Returns:
  a newly created sorted List

### sort

    public List sort(Closure closure)

  Sorts this Collection using the given closure as a comparator. The closure is passed each item from the collection, and is assumed to return a comparable value (i.e. an int).

Parameters:
  `closure` - a Closure used as a comparator.
Returns:
  a newly created sorted List

### sum

    public Object sum()

  Sums the items in a collection. This is equivalent to invoking the "plus" method on all items in the collection.

Returns:
  The sum of all of the items

### sum

    public Object sum(Object initialValue)

  Sums the items in a collection, adding the result to some initial value.

Parameters:
  `initialValue` - the items in the collection will be summed to this initial value.
Returns:
  The sum of all of the collection items.

### sum

    public Object sum(Closure closure)

  Sums the result of apply a closure to each item of a collection. `coll.sum(closure)` is equivalent to: `coll.collect(closure).sum()`.

Parameters:
  `closure` - a single parameter closure that returns a numeric value..
Returns:
  The sum of the values returned by applying the closure to each item of the list.

### sum

    public Object sum(Object initialValue, Closure closure)

  Sums the result of apply a closure to each item of a collection to sum intial value. `coll.sum(closure)` is equivalent to: `coll.collect(closure).sum()`.

Parameters:
  `closure` - a single parameter closure that returns a numeric value..
  `initialValue` - the closure results will be summed to this initial value.
Returns:
  The sum of the values returned by applying the closure to each item of the list.

### toList

    public List toList()

  Convert a collection to a List.

Returns:
  a List

### toListString

    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

### toString

    public String toString()

  Returns the string representation of the given collection. The string displays the contents of the collection, i.e. `[1, 2, a]`.

Returns:
  the string representation

See:
  #toListString(Collection).

### transpose

    public List transpose()

  Adds GroovyCollections#transpose(Collection) as a method on collections.

Returns:
  a List of the transposed lists

See:
  GroovyCollections#transpose.

### unique

    public Collection unique()

  Modifies this collection to remove all duplicated items, using the default comparator.

Returns:
  the now modified collection

### unique

    public Collection unique(Closure closure)

  A convenience method for making a collection unique using a closure as a comparator. 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 {@link Comparable#compareTo(Object)} 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).

Parameters:
  `closure` - a Closure used as a comparator.
Returns:
  self without any duplicates

### unique

    public Collection unique(Comparator comparator)

  Remove all duplicates from a given Collection. Works on the receiver object and 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
      public String toString() {
      return fname + " " + lname
      }
      }

      class PersonComparator implements Comparator {
      public 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)
      }

      public 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

Overview | Package | Class | Tree | Deprecated | Index | Help
| Groovy JDK