Mention mutating with List.sort

This commit is contained in:
Tim Yates 2013-09-02 12:59:18 +01:00
parent 68f26804c5
commit 6f444bece4

View File

@ -51,6 +51,7 @@ println x
/* /*
Collections and maps Collections and maps
*/ */
//Creating an empty list //Creating an empty list
def technologies = [] def technologies = []
@ -81,9 +82,12 @@ technologies.eachWithIndex { it, i -> println "$i: $it"}
technologies.contains('Groovy') technologies.contains('Groovy')
technologies.containsAll(['Groovy','Grails']) technologies.containsAll(['Groovy','Grails'])
//Sort a list // Sort a list (mutates original list)
technologies.sort() technologies.sort()
// To sort without mutating original, you can do:
sortedTechnologies = technologies.sort( false )
//Replace all elements in the list //Replace all elements in the list
Collections.replaceAll(technologies, 'Gradle', 'gradle') Collections.replaceAll(technologies, 'Gradle', 'gradle')