groovy
Insamlingsoperatörer
Sök…
Iterera över en samling
listor
def lst = ['foo', 'bar', 'baz']
// using implicit argument
lst.each { println it }
// using explicit argument
lst.each { val -> println val }
// both print:
// foo
// bar
// baz
Iterera med index
def lst = ['foo', 'bar', 'baz']
// explicit arguments are required
lst.eachWithIndex { val, idx -> println "$val in position $idx" }
// prints:
// foo in position 0
// bar in position 1
// baz in position 2
Kartor
def map = [foo: 'FOO', bar: 'BAR', baz: 'BAZ']
// using implicit argument
map.each { println "key: ${it.key}, value: ${it.value}"}
// using explicit arguments
map.each { k, v -> println "key: $k, value: $v"}
// both print:
// key: foo, value: FOO
// key: bar, value: BAR
// key: baz, value: BAZ
Skapa en ny lista med samla
def lst = ['foo', 'bar', 'baz']
lst.collect { it } // ['foo', 'bar', 'baz']
lst.collect { it.toUpperCase() } // ['FOO', 'BAR', 'BAZ']
För att samla nycklar eller värden från kartor
def map = [foo: 'FOO', bar: 'BAR', baz: 'BAZ']
def keys = map.collect { it.key } // ['foo', 'bar', 'baz']
def vals = map.collect { it.value } // ['FOO', 'BAR', 'BAZ']
Exemplet ovan motsvarar att ringa map.keySet()
och map.values()
Filtrera en lista med findAll
def lst = [10, 20, 30, 40]
lst.findAll { it > 25 } // [30, 40]
Hitta det första elementet som matchar ett villkor
def lst = [10, 20, 30, 40]
lst.find { it > 25 } // 30. Note: it returns a single value
Skapa kartor med collectEntries
Från listor
def lst = ['foo', 'bar', 'baz']
// for each entry return a list containing [key, value]
lst.collectEntries { [it, it.toUpperCase()] } // [foo: FOO, bar: BAR, baz: BAZ]
// another option, return a map containing the single entry
lst.collectEntries { [(it): it.toUpperCase()] } // [foo: FOO, bar: BAR, baz: BAZ]
Från kartor
def map = [foo: 'FOO', bar: 'BAR', baz: 'BAZ']
map.collectEntries { [it.key*2, it.value*2] } // [foofoo: FOOFOO, barbar: BARBAR, bazbaz: BAZBAZ]
// using explicit arguments k and v
map.collectEntries { k, v -> [k*2, v*2] } // [foofoo: FOOFOO, barbar: BARBAR, bazbaz: BAZBAZ]
Tillämpa transformation på kapslade samlingar
Tillämpa omvandlingen till poster som inte är samlade, dela in i kapslade samlingar och bevara hela strukturen.
def lst = ['foo', 'bar', ['inner_foo', 'inner_bar']]
lst.collectNested { it.toUpperCase() } // [FOO, BAR, [INNER_FOO, INNER_BAR]]
Platta en kapslad lista
def lst = ['foo', 'bar', ['inner_foo', 'inner_bar']]
lst.flatten() // ['foo', 'bar', 'inner_foo', 'inner_bar']
Ta bort dubbletter
def lst = ['foo', 'foo', 'bar', 'baz']
// *modifies* the list removing duplicate items
lst.unique() // [foo, bar, baz]
// setting to false the "mutate" argument returns a new list, leaving the original intact
lst.unique(false) // [foo, bar, baz]
// convert the list to a Set, thus removing duplicates
lst.toSet() // [baz, bar, foo]
// defining a custom equality criteria. For example: to elements are equal if have the same first letter
println lst.unique() { it[0] } // [foo, bar]. 'bar' and 'baz' considered equal
Bygg en karta från två listor
nrs = [1, 2, 3, 4, 5, 6, 7, 8, 9]
lets = ['a', 'b', 'c', 'd', 'e', 'f']
println GroovyCollections.transpose([nrs, lets])
.collect {le -> [(le[0]):le[1]]}.collectEntries { it }
or
println [nrs,lets].transpose().collectEntries{[it[0],it[1]]}
// [1:a, 2:b, 3:c, 4:d, 5:e, 6:f]
Modified text is an extract of the original Stack Overflow Documentation
Licensierat under CC BY-SA 3.0
Inte anslutet till Stack Overflow