|
Groovy JDK |
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 |
---|
public List drop(int num)
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) == []
num
- the number of elements to drop from this Iterable.num
elements,
or an empty list if it has less then num
elements.public List dropWhile(Closure condition)
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']
condition
- the closure that must evaluate to true to continue dropping elements.public Object first()
def set = [3, 4, 2] as LinkedHashSet assert set.first() == 3 // check original is unaltered assert set == [3, 4, 2] as SetThe 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.
public Object last()
def set = [3, 4, 2] as LinkedHashSet assert set.last() == 2 // check original unaltered assert set == [3, 4, 2] as SetThe 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.
public List take(int num)
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']
num
- the number of elements to take from this Iterable.num
elements from this Iterable,
or else all the elements from the Iterable if it has less then num
elements.public List takeWhile(Closure condition)
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']
condition
- the closure that must evaluate to true to
continue taking elements.public List toList()
|
Groovy JDK |