|
Groovy JDK |
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 |
---|
public Map collectEntries(Closure transform)
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.public Map collectEntries()
public Map collectEntries(Map collector, Closure transform)
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.public Map collectEntries(Map collector)
collector
- the Map into which the transformed entries are put.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 getAt(int idx)
// 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
idx
- an index value (-self.size() <= idx < self.size()) but using -ve index values will be inefficient.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 |