Groovy JDK

java.lang
Class Iterable

Method Summary
Map collectEntries(Closure transform)
A variant of collectEntries for Iterables.
Map collectEntries()
A variant of collectEntries for Iterables using the identity closure as the transform.
Map collectEntries(Map collector, Closure transform)
A variant of collectEntries for Iterables using a supplied map as the destination of transformed entries.
Map collectEntries(Map collector)
A variant of collectEntries for Iterables using the identity closure as the transform and a supplied map as the destination of transformed entries.
List drop(int num)
Drops the given number of elements from the head of this Iterable.
List dropWhile(Closure condition)
Returns a suffix of this Iterable where elements are dropped from the front while the given closure evaluates to true.
Object first()
Returns the first item from the Iterable.
Object getAt(int idx)
Support the subscript operator for an Iterable.
Object last()
Returns the last item from the Iterable.
List take(int num)
Returns the first num elements from the head of this Iterable.
List takeWhile(Closure condition)
Returns a List containing the longest prefix of the elements from this Iterable where each element passed to the given closure evaluates to true.
List toList()
Convert an Iterable to a List.
 
Method Detail

collectEntries

public Map collectEntries(Closure transform)
 
A variant of collectEntries for Iterables.
Parameters:
transform - the closure used for transforming, which has an item from self as the parameter and should return a Map.Entry, a Map or a two-element list containing the resulting key and value.
Returns:
a Map of the transformed entries
Since:
1.8.7
See:
Iterator#collectEntries(Closure).

collectEntries

public Map collectEntries()
 
A variant of collectEntries for Iterables using the identity closure as the transform.
Returns:
a Map of the transformed entries
Since:
1.8.7
See:
Iterator#collectEntries().

collectEntries

public Map collectEntries(Map collector, Closure transform)
 
A variant of collectEntries for Iterables using a supplied map as the destination of transformed entries.
Parameters:
collector - the Map into which the transformed entries are put.
transform - the closure used for transforming, which has an item from self as the parameter and should return a Map.Entry, a Map or a two-element list containing the resulting key and value.
Returns:
the collector with all transformed values added to it
Since:
1.8.7
See:
Iterator#collectEntries(Map, Closure).

collectEntries

public Map collectEntries(Map collector)
 
A variant of collectEntries for Iterables using the identity closure as the transform and a supplied map as the destination of transformed entries.
Parameters:
collector - the Map into which the transformed entries are put.
Returns:
the collector with all transformed values added to it
Since:
1.8.7
See:
Iterator#collectEntries(Map).

drop

public List drop(int num)
 
Drops the given number of elements from the head of this Iterable.
class AbcIterable implements Iterable {
    Iterator iterator() { "abc".iterator() }
}
def abc = new AbcIterable()
assert abc.drop(0) == ['a', 'b', 'c']
assert abc.drop(1) == ['b', 'c']
assert abc.drop(3) == []
assert abc.drop(5) == []
Parameters:
num - the number of elements to drop from this Iterable.
Returns:
a List consisting of all the elements of this Iterable minus the first num elements, or an empty list if it has less then num elements.
Since:
1.8.7

dropWhile

public List dropWhile(Closure condition)
 
Returns a suffix of this Iterable where elements are dropped from the front while the given closure evaluates to true.
class AbcIterable implements Iterable {
    Iterator iterator() { "abc".iterator() }
}
def abc = new AbcIterable()
assert abc.dropWhile{ it < 'b' } == ['b', 'c']
assert abc.dropWhile{ it <= 'b' } == ['c']
Parameters:
condition - the closure that must evaluate to true to continue dropping elements.
Returns:
the shortest suffix of the given Iterable such that the given closure condition evaluates to true for each element dropped from the front of the Iterable
Since:
1.8.7

first

public Object first()
 
Returns the first item from the Iterable.
def set = [3, 4, 2] as LinkedHashSet
assert set.first() == 3
// check original is unaltered
assert set == [3, 4, 2] as Set
The first element returned by the Iterable's iterator is returned. If the Iterable doesn't guarantee a defined order it may appear like a random element is returned.
Returns:
the first item from the Iterable
Since:
1.8.7

getAt

public Object getAt(int idx)
 
Support the subscript operator for an Iterable. Typical usage:
// custom Iterable example:
class MyIterable implements Iterable {
  Iterator iterator() { [1, 2, 3].iterator() }
}
def myIterable = new MyIterable()
assert myIterable[1] == 2

// Set example:
def set = [1,2,3] as LinkedHashSet
assert set[1] == 2
Parameters:
idx - an index value (-self.size() <= idx < self.size()) but using -ve index values will be inefficient.
Returns:
the value at the given index (after normalisation) or null if no corresponding value was found
Since:
2.1.0

last

public Object last()
 
Returns the last item from the Iterable.
def set = [3, 4, 2] as LinkedHashSet
assert set.last() == 2
// check original unaltered
assert set == [3, 4, 2] as Set
The first element returned by the Iterable's iterator is returned. If the Iterable doesn't guarantee a defined order it may appear like a random element is returned.
Returns:
the first item from the Iterable
Since:
1.8.7

take

public List take(int num)
 
Returns the first num elements from the head of this Iterable.
class AbcIterable implements Iterable {
    Iterator iterator() { "abc".iterator() }
}
def abc = new AbcIterable()
assert abc.take(0) == []
assert abc.take(1) == ['a']
assert abc.take(3) == ['a', 'b', 'c']
assert abc.take(5) == ['a', 'b', 'c']
Parameters:
num - the number of elements to take from this Iterable.
Returns:
a List consisting of the first num elements from this Iterable, or else all the elements from the Iterable if it has less then num elements.
Since:
1.8.7

takeWhile

public List takeWhile(Closure condition)
 
Returns a List containing the longest prefix of the elements from this Iterable where each element passed to the given closure evaluates to true.
class AbcIterable implements Iterable {
    Iterator iterator() { "abc".iterator() }
}
def abc = new AbcIterable()
assert abc.takeWhile{ it < 'b' } == ['a']
assert abc.takeWhile{ it <= 'b' } == ['a', 'b']
Parameters:
condition - the closure that must evaluate to true to continue taking elements.
Returns:
a List containing a prefix of the elements from the given Iterable where each element passed to the given closure evaluates to true
Since:
1.8.7

toList

public List toList()
 
Convert an Iterable to a List. The Iterable's iterator will become exhausted of elements after making this conversion.
Returns:
a List
Since:
1.8.7

Groovy JDK