Lists
A list is an ordered collection of objects:
...
| Code Block |
|---|
def list, list2, list3 list= [1, 2, list, 4] list2= [1, 2, list2, 4] assert list.equals(list2) list3= [1, 2, list, 4] assert ! list.equals(list3) |
Ranges and List-Slicing
Ranges are consecutive lists of sequential values like Integers, and can be used just like a List:
...
| Code Block |
|---|
def list = ['a','b','c','d','e','f','g'] list.putAt(2..3, 'z') assert list == ['a', 'b', 'z', 'e', 'f', 'g'] list.putAt(4..4, ['u','v']) assert list == ['a', 'b', 'z', 'e', 'u', 'v', 'g'] list.putAt(1..<3, []) assert list == ['a', 'e', 'u', 'v', 'g'] list.putAt( 0..<0, 'm' ) // assert list == ['m', 'a', 'e', 'u', 'v', 'g'] list.removeRange(1,3) //another method to do similar, means: list[1..<3]= [] list[1..2].clear() assert list == ['m', 'g'] |
More List Utilities
To reverse a list:
| Code Block |
|---|
assert [1,2,3].reverse() == [3,2,1]
def list= ['a','b','c','d','e']
Collections.reverse( list )
assert list == ['e','d','c','b','a']
use(Collections){ list.reverse() }
//alternative syntax for null-returning Collections.reverse(List)
assert list == ['a','b','c','d','e']
def list2= []
[1,2,3,4,5].reverseEach{ list2 << it*2 }
//same as, but more efficient than: [...].reverse().each{...}
assert list2 == [10,8,6,4,2]
assert [1,2,3,4,5,6][3..1] == [4,3,2]
//use backwards range to reverse returned sublist
def list3 = [1, 2, -3, 5, 6, -7, 9]
def rmc= Collections.reverseOrder()
Collections.sort(list3, rmc)
assert list3 == [9, 6, 5, 2, 1, -3, -7]
def list4 = [1, 2, -3, 5, 6, -7, 9]
def mc= [
compare: {a,b-> a.equals(b)? 0: Math.abs(a)<Math.abs(b)? -1: 1}
] as Comparator
def rmc2= Collections.reverseOrder( mc )
Collections.sort(list4, rmc2)
assert list4 == [9, -7, 6, 5, -3, 2, 1]
|
...
| Code Block |
|---|
def list= ['a', 7, 'b', 9, 7, 7, 2.4, 7]
Collections.replaceAll( list, 7, 55)
assert list == ['a', 55, 'b', 9, 55, 55, 2.4, 55]
list= ['a', 7, 'b', 9, 7, 7, 2.4, 7]
use(Collections){ list.replaceAll(7, 55) } //alternative syntax
assert list == ['a', 55, 'b', 9, 55, 55, 2.4, 55]
list= ['a',2,null,4,'zyx',2.5]
use(Collections){ list.fill( 'g' ) } //or: Collections.fill( list, 'g' )
assert list == ['g', 'g', 'g', 'g', 'g', 'g']
list= ['a', 'e', 'i', 'o', 'u', 'z']
use(Collections){ list.swap(2, 4) } //or: Collections.swap(list, 2, 4)
assert list == ['a', 'e', 'u', 'o', 'i', 'z']
assert Collections.frequency(['a','b','a','c','a','a','d','e'], 'a') == 4
use(Collections){
assert ['a','b','a','c','a','a','d','e'].frequency('a') == 4
}
list= ['a','b','c','d','e']
Collections.rotate(list, 3)
assert list == ['c','d','e','a','b']
use(Collections){ list.rotate(-2) }
assert list == ['e','a','b','c','d']
list= [1,2,3,4,5]
Collections.shuffle(list, new Random())
//we can supply our own random number generator...
assert list != [1,2,3,4,5]
list= [1,2,3,4,5]
Collections.shuffle(list) //...or use the default one
assert list != [1,2,3,4,5]
assert [3,5,5,5,2].unique() == [3,5,2]
def mc= [ compare:
{a,b-> a.equals(b) || a.equals(-b)? 0: Math.abs(a)<Math.abs(b)? -1: 1 }
] as Comparator
assert [3,5,5,-5,2,-7].unique(mc) == [3,5,2,-7]
//remove subsequent items comparator considers equal
assert [3,5,5,-5,2,-7].unique{a, b->
a == b || a == -b? 0: Math.abs(a)<Math.abs(b)? -1: 1
} == [3,5,2,-7]
list= [1,2,3]
Collections.copy( list, [9,8,7] )
assert list == [9,8,7] //overwrites original data
Collections.copy( list, [11,12] ) //source list shorter...
assert list == [11,12,7] //...which leaves remaining entries unchanged
try{ Collections.copy( list, [21,22,23,24] ); assert 0 } //source list too long
catch(e){ assert e instanceof IndexOutOfBoundsException }
list= [1,8,8,2,3,7,6,4,6,6,2,3,7,5]
assert Collections.indexOfSubList( list, [2,3,7] ) == 3
assert Collections.lastIndexOfSubList( list, [2,3,7] ) == 10
assert Collections.indexOfSubList( list, [9,9,13] ) == -1
//if sublist doesn't exist
|
Sets
A set is an unordered collection of objects, with no duplicates. It can be considered as a list with restrictions, and is often constructed from a list:
...
| Code Block |
|---|
def s= [1,2] as Set assert s.add(3) assert ! s.add(2) assert s.addAll( [5,4] ) assert ! s.addAll( [5,4] ) assert s == [1,2,3,5,4] as Set |
Examples with Lists and Sets
For small numbers of items, it's common in Groovy to use a list for set processing, and only convert it to a set when necessary, eg, for output.
...
| Code Block |
|---|
def s1=[1,2,3,4,5,6], s2=[4,5,6,7,8,9] def diff = (s1 as Set) + s2 tmp = s1 as Set tmp.retainAll(s2) diff.removeAll(tmp) assert diff == [1,2,3,7,8,9] |
Sorted Sets
A sorted set is one with extra methods that utilize the sorting of the elements. It's often more efficient than doing the same with lists.
...
| Code Block |
|---|
Object predecessor = ss.headSet( elt ).last() |
Immutable Collections
We can convert a list or set into one that can't be modified:
...