- Gets the elements in the list collection
def LST = [1,3,4,1,8,9,2,6]println lst[-1]println lst[-2] Output: output: 62
- Use Range object to get several consecutive values in a collection
Take value from left to right
def LST = [1,3,4,1,8,9,2,6]println lst[2..5] Output Result: [4, 1, 8, 9]
Value from right to left
def LST = [1,3,4,1,8,9,2,6]println lst[-1..-4]
Output results: [6, 2, 9, 8]
- Iterative ArrayList
Iterate from left to right in LST = [1,3,4,1,8,9,2,6]lst.each{print "${it},"} Output: 1,3,4,1,8,9,2,6,//iterates from right to left in the reverse direction of LST = [1,3,4,1,8,9,2,6] lst.each{print "${it},"} Output: 6,2,9,8,1,4,3,1,//Iteration Display index def LST = [1,3,4,1,8,9,2,6]lst.eachwithindex{it,i, print ( "${i},")} Output Result: 0,1,2,3,4,5,6,7,
- Using the Collect method of list
Finding the list element/*find () finds the first occurrence of the match object, and it iterates only until the closure returns true. The resulting True,find method stops the iteration and returns the current element. Returns null if the traversal ends and does not get true. */lst = [1,3,4,1,8,9,2,6]println lst.find{it > 4} output Result: 8//Find list element, return all eligible elements LST = [1,3,4,1,8,9,2,6]println lst.findall{it > 4} output result: [8,9,6]//Find list element, return element subscript lst = [1,3,4,1,8,9,2,6]println lst.findallindexof{it = = 4} output Result: 2
- Sort by using list
def ids = [5,6,3,7,1,4,9] //Can be considered Comparator sort ids.sort {a,b-> return-a.compareto (b) } println IDs //Natural sort ids.sort (); println IDs output: [9, 7, 6, 5, 4, 3, 1][1, 3, 4, 5, 6, 7, 9]
- List to go heavy
LST = [1,3,1,1,8,9,2,6]println lst.unique () output result: [1, 3, 8, 9, 2, 6]
- Link a list element to a string
LST = [' Shun Fenghai ', ' is good ', ' only sell genuine ']println lst.join (') println lst.join (', ') output: Shun Fenghai is good only sell genuine shun Fenghai, is good, only sell authentic
- Element substitution
LST = [' Shun Fenghai ', ' is good ', ' sell only authentic ']lst[0] = [' sfht ', '. com ']println lst.flatten () output: [Sfht,. com, that's good, sell only authentic]
- +/-Operator
LST = [' Shun Fenghai ', ' is good ', ' only sell genuine ']println LST-[' is good '] output: [Shun Fenghai, only sell genuine]lst = [' Shun Fenghai ', ' is good ', ' sell genuine ']println lst + [' You say it '] output: [Shun Fung Sea Amoy, is good, only sells the genuine, you say?
- List element Leveling
LST = [[up], ' Shun Fenghai ', ' is good ', ' only sell authentic ']println lst.flatten () output: [1, 2, ' Shun Fenghai ', ' is good ', ' only sell genuine ']