---
title: Iterable<E>
description: Reference for the Iterable trait, implemented by collection types that support iteration.
---

# Iterable\<E\>

*Implemented by the following types: [Bag\<E\>][stdlib-types-bag], [List\<E\>][stdlib-types-list], [Set\<E\>][stdlib-types-set]*

`Iterable` is a trait implemented by collection types that support iteration.

Types that implement `Iterable` can be passed as an `Iterable` parameter of a method, and they can use all of the methods described below.

The following methods are available for iterables:

##### `any( condition: (E) -> Boolean ) -> Boolean`

Returns `true` if any element in the iterable satisfies the given condition.

##### `collect( transform: (E) -> R ) -> Iterable<R>`

Returns a new iterable with each element transformed by the given closure.

##### `collectMany( transform: (E) -> Iterable<R> ) -> Iterable<R>`

Transforms each element in the iterable into a collection with the given closure and concatenates the resulting collections into a list.

##### `contains( value: E ) -> Boolean`

Returns `true` if the iterable contains the given value.

##### `each( action: (E) -> () )`

Invokes the given closure for each element in the iterable.

##### `every( condition: (E) -> Boolean ) -> Boolean`

Returns `true` if every element in the iterable satisfies the given condition.

##### `find( condition: (E) -> Boolean ) -> E`

Returns the first element in the iterable that satisfies the given condition.

##### `findAll( condition: (E) -> Boolean ) -> Iterable<E>`

Returns the elements in the iterable that satisfy the given condition.

##### `groupBy( transform: (E) -> K ) -> Map<K,Iterable<E>>`

Collect the elements of an iterable into groups based on a matching key. The closure should return the key for a given element.

##### `inject( accumulator: (E,E) -> E ) -> E`

Apply the given accumulator to each element in the iterable and return the final accumulated value. The closure should accept two parameters, corresponding to the current accumulated value and the current iterable element, and return the next accumulated value.
The first element from the iterable is used as the initial accumulated value.

##### `inject( initialValue: R, accumulator: (R,E) -> R ) -> R`

Apply the given accumulator to each element in the iterable and return the final accumulated value. The closure should accept two parameters, corresponding to the current accumulated value and the current iterable element, and return the next accumulated value.

##### `isEmpty() -> Boolean`

Returns `true` if the iterable is empty.

##### `join( separator: String = '' ) -> String`

Concatenates the string representation of each element in the iterable, with the given string as the separator between each element.

##### `max() -> E`

Returns the maximum element in the iterable.

##### `max( comparator: (E) -> R ) -> E`

Returns the maximum element in the iterable according to the given closure.
The closure should follow the same semantics as the closure parameter of `toSorted()`.

##### `max( comparator: (E,E) -> Integer ) -> E`

Returns the maximum element in the iterable according to the given closure.
The closure should follow the same semantics as the closure parameter of `toSorted()`.

##### `min() -> E`

Returns the minimum element in the iterable.

##### `min( comparator: (E) -> R ) -> E`

Returns the minimum element in the iterable according to the given closure.
The closure should follow the same semantics as the closure parameter of `toSorted()`.

##### `min( comparator: (E,E) -> Integer ) -> E`

Returns the minimum element in the iterable according to the given closure.
The closure should follow the same semantics as the closure parameter of `toSorted()`.

##### `size() -> Integer`

Returns the number of elements in the iterable.

##### `sum() -> E`

Returns the sum of the elements in the iterable. The elements should support addition (`+`).

##### `sum( transform: (E) -> R ) -> R`

Transforms each element in the iterable with the given closure and returns the sum. The values returned by the closure should support addition (`+`).

##### `toList() -> List<E>`

Converts the iterable to a list.
:::danger
Converting an unordered collection to a list can lead to non-deterministic behavior. Consider using `toSorted()` instead to ensure a deterministic ordering. See [non-deterministic process inputs][cache-nondeterministic-inputs] for more information.
:::

##### `toSet() -> Set<E>`

Converts the iterable to a set. Duplicate elements are excluded.

##### `toSorted() -> List<E>`

Returns a sorted list of the iterable's elements.

##### `toSorted( comparator: (E) -> R ) -> List<E>`

Returns the iterable as a list sorted according to the given closure.
The closure should accept one parameter and transform each element into the value that will be used for comparisons.

##### `toSorted( comparator: (E,E) -> Integer ) -> List<E>`

Returns the iterable as a list sorted according to the given closure.
The closure should accept two parameters and return a negative integer, zero, or a positive integer to denote whether the first argument is less than, equal to, or greater than the second.

##### `toUnique() -> Iterable<E>`

Returns a shallow copy of the iterable with duplicate elements excluded.

##### `toUnique( comparator: (E) -> R ) -> Iterable<E>`

##### `toUnique( comparator: (E,E) -> Integer ) -> Iterable<E>`

Returns a shallow copy of the iterable with duplicate elements excluded.
The closure should follow the same semantics as the closure parameter of `toSorted()`.

:::note
Iterables in Nextflow are backed by the [Java](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Iterable.html) and [Groovy](https://docs.groovy-lang.org/latest/html/groovy-jdk/java/lang/Iterable.html) standard libraries, which may expose additional methods. Only methods which are recommended for use in Nextflow are documented here.
:::

[cache-nondeterministic-inputs]: ../../cache-and-resume#non-deterministic-process-inputs
[stdlib-types-bag]: ./bag
[stdlib-types-list]: ./list
[stdlib-types-set]: ./set
