Groovy JDK

java.lang
Class Iterable

Method Summary
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 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

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

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