|
Groovy JDK |
Method Summary | |
---|---|
boolean
|
asBoolean()
Coerce an iterator instance to a boolean value. |
Map
|
collectEntries(Closure transform)
A variant of collectEntries for Iterators. |
Map
|
collectEntries()
A variant of collectEntries for Iterators using the identity closure as the transform. |
Map
|
collectEntries(Map collector, Closure transform)
A variant of collectEntries for Iterators using a supplied map as the destination of transformed entries. |
Map
|
collectEntries(Map collector)
A variant of collectEntries for Iterators using the identity closure as the transform and a supplied map as the destination of transformed entries. |
List
|
collectMany(Closure projection)
Projects each item from a source iterator to a collection and concatenates (flattens) the resulting collections into a single list. |
Number
|
count(Object value)
Counts the number of occurrences of the given value from the items within this Iterator. |
Number
|
count(Closure closure)
Counts the number of occurrences which satisfy the given closure from the items within this Iterator. |
Map
|
countBy(Closure closure)
Sorts all iterator items into groups determined by the supplied mapping closure and counts the group size. |
Iterator
|
drop(int num)
Drops the given number of elements from the head of this iterator if they are available. |
Iterator
|
dropWhile(Closure condition)
Creates an Iterator that returns a suffix of the elements from an original Iterator. |
Object
|
getAt(int idx)
Support the subscript operator for an Iterator. |
Object
|
inject(Object initialValue, Closure closure)
Iterates through the given Iterator, passing in the initial value to the closure along with the first item. |
Iterator
|
iterator()
An identity function for iterators, supporting 'duck-typing' when trying to get an iterator for each object within a collection, some of which may already be iterators. |
String
|
join(String separator)
Concatenates the toString() representation of each
item from the iterator, with the given String as a separator between
each item.
|
Object
|
max()
Adds max() method to Iterator objects. |
Object
|
max(Closure closure)
Selects the maximum value found from the Iterator using the closure to determine the correct ordering. |
Object
|
max(Comparator comparator)
Selects the maximum value found from the Iterator using the given comparator. |
Object
|
min()
Adds min() method to Iterator objects. |
Object
|
min(Comparator comparator)
Selects the minimum value found from the Iterator using the given comparator. |
Object
|
min(Closure closure)
Selects the minimum value found from the Iterator using the closure to determine the correct ordering. |
Iterator
|
reverse()
Reverses the iterator. |
int
|
size()
Provide the standard Groovy size() method for Iterator .
|
Iterator
|
sort()
Sorts the given iterator items into a sorted iterator. |
Iterator
|
sort(Comparator comparator)
Sorts the given iterator items into a sorted iterator using the comparator. |
Iterator
|
sort(Closure closure)
Sorts the given iterator items into a sorted iterator using the Closure to determine the correct ordering. |
Object
|
sum()
Sums the items from an Iterator. |
Object
|
sum(Object initialValue)
Sums the items from an Iterator, adding the result to some initial value. |
Object
|
sum(Closure closure)
Sums the result of apply a closure to each item returned from an iterator. |
Object
|
sum(Object initialValue, Closure closure)
Sums the result of applying a closure to each item of an Iterator to some initial value. |
Iterator
|
take(int num)
Returns an iterator of up to the first num elements from this iterator.
|
Iterator
|
takeWhile(Closure condition)
Returns the longest prefix of elements in this iterator where each element passed to the given condition closure evaluates to true. |
List
|
toList()
Convert an iterator to a List. |
Set
|
toSet()
Convert an iterator to a Set. |
Iterator
|
unique()
Returns an iterator equivalent to this iterator all duplicated items removed by using the default comparator. |
Iterator
|
unique(Closure closure)
Returns an iterator equivalent to this iterator but with all duplicated items removed by using a Closure to determine duplicate (equal) items. |
Iterator
|
unique(Comparator comparator)
Returns an iterator equivalent to this iterator with all duplicated items removed by using the supplied comparator. |
Method Detail |
---|
public boolean asBoolean()
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 collectMany(Closure projection)
def numsIter = [1, 2, 3, 4, 5, 6].iterator() def squaresAndCubesOfEvens = numsIter.collectMany{ it % 2 ? [] : [it**2, it**3] } assert squaresAndCubesOfEvens == [4, 8, 16, 64, 36, 216]
projection
- a projecting Closure returning a collection of items.public Number count(Object value)
compareTo(value) == 0
or equals(value)
).
The iterator will become exhausted of elements after determining the count value.value
- the value being searched for.public Number count(Closure closure)
Example usage:
assert [2,4,2,1,3,5,2,4,3].toSet().iterator().count{ it % 2 == 0 } == 2
closure
- a closure condition.public Map countBy(Closure closure)
Example usage:
assert [1,2,2,2,3].toSet().iterator().countBy{ it % 2 } == [1:2, 0:1]
closure
- a closure mapping items to the frequency keys.public Iterator drop(int num)
num
elements.
def iteratorCompare( Iterator a, List b ) { a.collect { it } == b } def iter = [ 1, 2, 3, 4, 5 ].listIterator() assert iteratorCompare( iter.drop( 0 ), [ 1, 2, 3, 4, 5 ] ) iter = [ 1, 2, 3, 4, 5 ].listIterator() assert iteratorCompare( iter.drop( 2 ), [ 3, 4, 5 ] ) iter = [ 1, 2, 3, 4, 5 ].listIterator() assert iteratorCompare( iter.drop( 5 ), [] )
num
- the number of elements to drop from this iterator.num
elements if they exist.public Iterator dropWhile(Closure condition)
def a = 0 def iter = [ hasNext:{ a < 10 }, next:{ a++ } ] as Iterator assert [].iterator().dropWhile{ it < 3 }.toList() == [] assert [1, 2, 3, 4, 5].iterator().dropWhile{ it < 3 }.toList() == [ 3, 4, 5 ] assert iter.dropWhile{ it < 5 }.toList() == [ 5, 6, 7, 8, 9 ]
condition
- the closure that must evaluate to true to continue dropping elements.public Object getAt(int idx)
def iter = [2, "a", 5.3].iterator() assert iter[1] == "a"A more elaborate example:
def items = [2, "a", 5.3] def iter = items.iterator() assert iter[-1] == 5.3 // iter exhausted, so reset iter = items.iterator() assert iter[1] == "a" // iter partially exhausted so now idx starts after "a" assert iter[0] == 5.3
idx
- an index value (-self.size() <= idx < self.size()).public Object inject(Object initialValue, Closure closure)
initialValue
- some initial value.closure
- a closure.public Iterator iterator()
public String join(String separator)
toString()
representation of each
item from the iterator, with the given String as a separator between
each item. The iterator will become exhausted of elements after
determining the resulting conjoined value.separator
- a String separator.public Object max()
public Object max(Closure closure)
closure
- a Closure used to determine the correct ordering.public Object max(Comparator comparator)
comparator
- a Comparator.public Object min()
public Object min(Comparator comparator)
comparator
- a Comparator.public Object min(Closure closure)
closure
- a Closure used to determine the correct ordering.public Iterator reverse()
public int size()
size()
method for Iterator
.
The iterator will become exhausted of elements after determining the size value.public Iterator sort()
public Iterator sort(Comparator comparator)
comparator
- a Comparator used for comparing items.public Iterator sort(Closure closure)
closure
- a Closure used to determine the correct ordering.public Object sum()
public Object sum(Object initialValue)
initialValue
- the items in the collection will be summed to this initial value.public Object sum(Closure closure)
iter.sum(closure)
is equivalent to:
iter.collect(closure).sum()
. The iterator will become
exhausted of elements after determining the sum value.closure
- a single parameter closure that returns a numeric value..public Object sum(Object initialValue, Closure closure)
iter.sum(initVal, closure)
is equivalent to:
iter.collect(closure).sum(initVal)
. The iterator will become
exhausted of elements after determining the sum value.closure
- a single parameter closure that returns a numeric value..initialValue
- the closure results will be summed to this initial value.public Iterator take(int num)
num
elements from this iterator.
The original iterator is stepped along by num
elements.
def a = 0 def iter = [ hasNext:{ true }, next:{ a++ } ] as Iterator def iteratorCompare( Iterator a, List b ) { a.collect { it } == b } assert iteratorCompare( iter.take( 0 ), [] ) assert iteratorCompare( iter.take( 2 ), [ 0, 1 ] ) assert iteratorCompare( iter.take( 5 ), [ 2, 3, 4, 5, 6 ] )
num
- the number of elements to take from this iterator.num
elements of this iterator.public Iterator takeWhile(Closure condition)
def a = 0 def iter = [ hasNext:{ true }, next:{ a++ } ] as Iterator assert [].iterator().takeWhile{ it < 3 }.toList() == [] assert [1, 2, 3, 4, 5].iterator().takeWhile{ it < 3 }.toList() == [ 1, 2 ] assert iter.takeWhile{ it < 5 }.toList() == [ 0, 1, 2, 3, 4 ]
condition
- the closure that must evaluate to true to
continue taking elements.public List toList()
public Set toSet()
public Iterator unique()
public Iterator unique(Closure closure)
closure
- a Closure used to determine unique items.public Iterator unique(Comparator comparator)
comparator
- a Comparator.
|
Groovy JDK |