The DevOps Zone is brought to you in partnership with Sonatype Nexus. The Nexus Suite helps scale your DevOps delivery with continuous component intelligence integrated into development tools, including Eclipse, IntelliJ, Jenkins, Bamboo, SonarQube and more. Schedule a demo today.
Groovy already has so many extra methods for working with collections. If we have to need to swap two elements in a collection we can use the
swap method. We provide the two index values of the elements we want to swap and Groovy swaps the elements.
In the following sample we have a simple list and swap all elements by invoking the
swap method two times:
def saying = ['Groovy', 'is', 'great']
def yodaSays = saying.swap(2, 1).swap(0, 1)
assert yodaSays.join(' ') == 'great Groovy is'
Written with Groovy 2.4.1.
The DevOps Zone is brought to you in partnership with Sonatype Nexus. Use the Nexus Suite to automate your software supply chain and ensure you're using the highest quality open source components at every step of the development lifecycle. Get Nexus today.