Zoeken…


Herhaal een verzameling

lijsten

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

Herhaal met 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

Kaarten

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

Maak een nieuwe lijst met behulp van verzamelen

def lst = ['foo', 'bar', 'baz']
lst.collect { it } // ['foo', 'bar', 'baz']

lst.collect { it.toUpperCase() } // ['FOO', 'BAR', 'BAZ']

Om sleutels of waarden van kaarten te verzamelen

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']

Het bovenstaande voorbeeld is gelijk aan het aanroepen van map.keySet() en map.values()

Filter een lijst met findAll

def lst = [10, 20, 30, 40]

lst.findAll { it > 25 } // [30, 40]

Zoek het eerste element dat overeenkomt met een voorwaarde

def lst = [10, 20, 30, 40]

lst.find { it > 25 } // 30. Note: it returns a single value

Maak kaarten met collectEntries

Uit lijsten

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]

Van kaarten

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]

Pas transformatie toe op geneste collecties

Pas de transformatie toe op items die geen verzameling zijn, ga ook in op geneste collecties en behoud de hele structuur.

def lst = ['foo', 'bar', ['inner_foo', 'inner_bar']]

lst.collectNested { it.toUpperCase() } // [FOO, BAR, [INNER_FOO, INNER_BAR]]

Een geneste lijst afvlakken

def lst = ['foo', 'bar', ['inner_foo', 'inner_bar']]

lst.flatten() ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​// ['foo', 'bar', 'inner_foo', 'inner_bar']

Duplicaten verwijderen

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

Maak een kaart op basis van twee lijsten

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
Licentie onder CC BY-SA 3.0
Niet aangesloten bij Stack Overflow