|
Groovy JDK |
Method Summary | |
---|---|
boolean
|
asBoolean()
Coerce a collection instance to a boolean value. |
Collection
|
asImmutable()
A convenience method for creating an immutable Collection. |
List
|
asList()
Converts this collection to a List. |
Collection
|
asSynchronized()
A convenience method for creating a synchronized Collection. |
Object
|
asType(Class)
Converts the given collection to another type. |
List
|
collect(Closure)
Iterates through this collection transforming each entry into a new value using the closure as a transformer, returning a list of transformed values. |
Collection
|
collect(Collection, Closure)
Iterates through this collection transforming each value into a new value using the closure as a transformer, returning an initial collection plus the transformed values. |
List
|
collectAll(Closure)
Recursively iterates through this collection transforming each non-Collection value into a new value using the closure as a transformer. |
Collection
|
collectAll(Collection, Closure)
Recursively iterates through this collection transforming each non-Collection value into a new value using the closure as a transformer. |
List
|
combinations()
Adds GroovyCollections#combinations(Collection) as a method on collections. |
Number
|
count(Object)
Counts the number of occurrences of the given value inside this collection. |
boolean
|
disjoint(Collection)
Returns true if the intersection of two collections is empty.
|
Iterator
|
eachPermutation(Closure)
Iterates over all permutations of a collection, running a closure for each iteration. |
Object
|
find(Closure)
Finds the first value matching the closure condition. |
Collection
|
findAll(Closure)
Finds all values matching the closure condition. |
Collection
|
flatten()
Flatten a collection. |
Collection
|
flatten(Closure)
Flatten a collection. |
List
|
getAt(String)
Support the subscript operator for List assert [String, Long, Integer] == ["a",5L,2]["class"] |
Map
|
groupBy(Closure)
Sorts all collection members into groups determined by the supplied mapping closure. |
Object
|
inject(Object, Closure)
Iterates through the given collection, passing in the initial value to the closure along with the current iterated item then passing into the next iteration the value of the previous closure. |
Collection
|
intersect(Collection)
Create a Collection composed of the intersection of both collections. |
boolean
|
isCase(Object)
'Case' implementation for collections which tests if the 'switch' operand is contained in any of the 'case' values. |
String
|
join(String)
Concatenates the toString() representation of each
item in this collection, with the given String as a separator between
each item.
|
Collection
|
leftShift(Object)
Overloads the left shift operator to provide an easy way to append objects to a Collection. |
Object
|
max()
Adds max() method to Collection objects. |
Object
|
max(Closure)
Selects the maximum value found in the collection using the closure to determine the correct ordering. |
Object
|
max(Comparator)
Selects the maximum value found in the collection using the given comparator. |
Object
|
min()
Adds min() method to Collection objects. |
Object
|
min(Comparator)
Selects the minimum value found in the collection using the given comparator. |
Object
|
min(Closure)
Selects the minimum value found in the collection using the closure to determine the correct ordering. |
List
|
multiply(Number)
Create a List composed of the elements of this list, repeated a certain number of times. |
Collection
|
plus(Collection)
Create a Collection as a union of two collections. |
Collection
|
plus(Object)
Create a collection as a union of a Collection and an Object. |
List
|
sort()
Sorts the given collection into a sorted list. |
List
|
sort(Comparator)
Sorts the Collection using the given comparator. |
List
|
sort(Closure)
Sorts this Collection using the closure to determine the correct ordering. |
Collection
|
split(Closure)
Splits all items into two collections based on the closure condition. |
Object
|
sum()
Sums the items in a collection. |
Object
|
sum(Object)
Sums the items in a collection, adding the result to some initial value. |
Object
|
sum(Closure)
Sums the result of apply a closure to each item of a collection. |
Object
|
sum(Object, Closure)
Sums the result of applying a closure to each item of a collection to some initial value. |
List
|
toList()
Convert a collection to a List. |
String
|
toListString()
Returns the string representation of the given list. |
Collection
|
unique()
Modifies this collection to remove all duplicated items, using the default comparator. |
Collection
|
unique(Closure)
A convenience method for making a collection unique using a Closure to determine duplicate (equal) items. |
Collection
|
unique(Comparator)
Remove all duplicates from a given Collection. |
Method Detail |
---|
public boolean asBoolean()
assert [1,2].asBoolean() == true
assert [].asBoolean() == false
public Collection asImmutable()
def mutable = [1,2,3] def immutable = mutable.asImmutable() mutable << 4 try { immutable << 4 assert false } catch (UnsupportedOperationException) { assert true }
public List asList()
assert new HashSet().asList() instanceof List
public Collection asSynchronized()
public Object asType(Class)
clazz
- the desired class.public List collect(Closure)
assert [2,4,6] == [1,2,3].collect { it * 2 }
closure
- the closure used for mapping.public Collection collect(Collection, Closure)
assert [1,2,3] as HashSet == [2,4,5,6].collect(new HashSet()) { (int)(it / 2) }
collection
- an initial Collection to which the transformed values are added.closure
- the closure used to transform each element of the collection.public List collectAll(Closure)
assert [2,[4,6],[8],[]] == [1,[2,3],[4],[]].collectAll { it * 2 }
closure
- the closure used to transform each element of the collection.public Collection collectAll(Collection, Closure)
def x = [1,[2,3],[4],[]].collectAll(new Vector()) { it * 2 } assert x == [2,[4,6],[8],[]] assert x instanceof Vector
collection
- an initial Collection to which the transformed values are added.closure
- the closure used to transform each element of the collection.public List combinations()
assert [['a', 'b'],[1, 2, 3]].combinations() == [['a', 1], ['b', 1], ['a', 2], ['b', 2], ['a', 3], ['b', 3]]
public Number count(Object)
compareTo(value) == 0
or equals(value)
).
assert [2,4,2,1,3,5,2,4,3].count(4) == 2
value
- the value being searched for.public boolean disjoint(Collection)
true
if the intersection of two collections is empty.
assert [1,2,3].disjoint([3,4,5]) == false
assert [1,2].disjoint([3,4]) == true
right
- a Collection.true
if the intersection of two collections
is empty, false
otherwise.public Iterator eachPermutation(Closure)
def permutations = [] [1, 2, 3].eachPermutation{ permutations << it } assert permutations == [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
closure
- the closure to call for each permutation.public Object find(Closure)
def list = [1,2,3] assert 2 == list.find { it > 1 }
closure
- a closure condition.public Collection findAll(Closure)
assert [2,4] == [1,2,3,4].findAll { it % 2 == 0 }
closure
- a closure condition.public Collection flatten()
assert [1,2,3,4,5] == [1,[2,3],[[4]],[],5].flatten()
public Collection flatten(Closure)
flattenUsing
- a closure to determine how to flatten non-Array, non-Collection elements.public List getAt(String)
assert [String, Long, Integer] == ["a",5L,2]["class"]
property
- a String.public Map groupBy(Closure)
assert [0:[2,4,6], 1:[1,3,5]] == [1,2,3,4,5,6].groupBy { it % 2 }
closure
- a closure mapping entries on keys.public Object inject(Object, Closure)
assert 1*1*2*3*4 == [1,2,3,4].inject(1) { acc, val -> acc * val }
value
- a value.closure
- a closure.public Collection intersect(Collection)
assert [4,5] == [1,2,3,4,5].intersect([4,5,6,7,8])
right
- a Collection.public boolean isCase(Object)
switch( 3 ) { case [1,3,5]: assert true break default: assert false }
switchValue
- the switch value.public String join(String)
toString()
representation of each
item in this collection, with the given String as a separator between
each item.
assert "1, 2, 3" == [1,2,3].join(", ")
separator
- a String separator.public Collection leftShift(Object)
def list = [1,2] list << 3 assert list == [1,2,3]
value
- an Object to be added to the collection..public Object max()
assert 5 == [2,3,1,5,4].max()
public Object max(Closure)
assert "hello" == ["hello","hi","hey"].max { it.length() }
assert "hello" == ["hello","hi","hey"].max { a, b -> a.length() <=> b.length() }
closure
- a 1 or 2 arg Closure used to determine the correct ordering.public Object max(Comparator)
assert "hello" == ["hello","hi","hey"].max( { a, b -> a.length() <=> b.length() } as Comparator )
comparator
- a Comparator.public Object min()
assert 2 == [4,2,5].min()
public Object min(Comparator)
assert "hi" == ["hello","hi","hey"].min( { a, b -> a.length() <=> b.length() } as Comparator )
comparator
- a Comparator.public Object min(Closure)
assert "hi" == ["hello","hi","hey"].min { it.length() }
closure
- a 1 or 2 arg Closure used to determine the correct ordering.public List multiply(Number)
assert [1,2,3,1,2,3] == [1,2,3] * 2
factor
- the number of times to append.public Collection plus(Collection)
assert [1,2,3,4] == [1,2] + [3,4]
right
- the right Collection.public Collection plus(Object)
assert [1,2,3] == [1,2] + 3
right
- an object to add/append.public List sort()
assert [1,2,3] == [3,1,2].sort()
public List sort(Comparator)
assert ["hi","hey","hello"] == ["hello","hi","hey"].sort( { a, b -> a.length() <=> b.length() } as Comparator )
comparator
- a Comparator used for the comparison.public List sort(Closure)
assert ["hi","hey","hello"] == ["hello","hi","hey"].sort { it.length() }
assert ["hi","hey","hello"] == ["hello","hi","hey"].sort { a, b -> a.length() <=> b.length() }
closure
- a 1 or 2 arg Closure used to determine the correct ordering.public Collection split(Closure)
assert [[2,4],[1,3]] == [1,2,3,4].split { it % 2 == 0 }
closure
- a closure condition.public Object sum()
assert 1+2+3+4 == [1,2,3,4].sum()
public Object sum(Object)
assert 5+1+2+3+4 == [1,2,3,4].sum(5)
initialValue
- the items in the collection will be summed to this initial value.public Object sum(Closure)
coll.sum(closure)
is equivalent to:
coll.collect(closure).sum()
.
assert 4+6+10+12 == [2,3,5,6].sum() { it * 2 }
closure
- a single parameter closure that returns a numeric value..public Object sum(Object, Closure)
coll.sum(initVal, closure)
is equivalent to:
coll.collect(closure).sum(initVal)
.
assert 50+4+6+10+12 == [2,3,5,6].sum(50) { it * 2 }
closure
- a single parameter closure that returns a numeric value..initialValue
- the closure results will be summed to this initial value.public List toList()
def x = [1,2,3] as HashSet assert x.class == HashSet assert x.toList() instanceof List
public String toListString()
[1, 2, a]
.public Collection unique()
assert [1,3] == [1,3,3].unique()
public Collection unique(Closure)
assert [1,4] == [1,3,4,5].unique { it % 2 }
assert [2,3,4] == [2,3,3,4].unique { a, b -> a <=> b }
closure
- a 1 or 2 arg Closure used to determine unique items.public Collection unique(Comparator)
class Person {
def fname, lname
public String toString() {
return fname + " " + lname
}
}
class PersonComparator implements Comparator {
public int compare(Object o1, Object o2) {
Person p1 = (Person) o1
Person p2 = (Person) o2
if (p1.lname != p2.lname)
return p1.lname.compareTo(p2.lname)
else
return p1.fname.compareTo(p2.fname)
}
public boolean equals(Object obj) {
return this.equals(obj)
}
}
Person a = new Person(fname:"John", lname:"Taylor")
Person b = new Person(fname:"Clark", lname:"Taylor")
Person c = new Person(fname:"Tom", lname:"Cruz")
Person d = new Person(fname:"Clark", lname:"Taylor")
def list = [a, b, c, d]
List list2 = list.unique(new PersonComparator())
assert( list2 == list && list == [a, b, c] )
comparator
- a Comparator.
|
Groovy JDK |