mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-23 09:41:36 +00:00
Another way of checking List.contains and some headers
This commit is contained in:
parent
6f444bece4
commit
95058aea96
@ -74,20 +74,33 @@ technologies.remove("Griffon")
|
|||||||
// Subtraction works also
|
// Subtraction works also
|
||||||
technologies = technologies - 'Grails'
|
technologies = technologies - 'Grails'
|
||||||
|
|
||||||
|
/*** Iterating Lists ***/
|
||||||
|
|
||||||
// Iterate over elements of a list
|
// Iterate over elements of a list
|
||||||
technologies.each { println "Technology: $it"}
|
technologies.each { println "Technology: $it"}
|
||||||
technologies.eachWithIndex { it, i -> println "$i: $it"}
|
technologies.eachWithIndex { it, i -> println "$i: $it"}
|
||||||
|
|
||||||
|
/*** Checking List contents ***/
|
||||||
|
|
||||||
//Evaluate if a list contains element(s) (boolean)
|
//Evaluate if a list contains element(s) (boolean)
|
||||||
technologies.contains('Groovy')
|
contained = technologies.contains( 'Groovy' )
|
||||||
|
|
||||||
|
// Or
|
||||||
|
contained = 'Groovy' in technologies
|
||||||
|
|
||||||
|
// Check for multiple contents
|
||||||
technologies.containsAll(['Groovy','Grails'])
|
technologies.containsAll(['Groovy','Grails'])
|
||||||
|
|
||||||
|
/*** Sorting Lists ***/
|
||||||
|
|
||||||
// Sort a list (mutates original list)
|
// Sort a list (mutates original list)
|
||||||
technologies.sort()
|
technologies.sort()
|
||||||
|
|
||||||
// To sort without mutating original, you can do:
|
// To sort without mutating original, you can do:
|
||||||
sortedTechnologies = technologies.sort( false )
|
sortedTechnologies = technologies.sort( false )
|
||||||
|
|
||||||
|
/*** Manipulating Lists ***/
|
||||||
|
|
||||||
//Replace all elements in the list
|
//Replace all elements in the list
|
||||||
Collections.replaceAll(technologies, 'Gradle', 'gradle')
|
Collections.replaceAll(technologies, 'Gradle', 'gradle')
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user