当前位置:网站首页>Groovy list operation

Groovy list operation

2022-06-22 14:37:00 Leisurely summer

1、 How lists are defined

Groovy There are two ways to define lists in , First, use. Java To create a list , The two is the use of Groovy Language to create lists

def list = new ArrayList() // java How to define in 
// Groovy How lists are defined in 
def list = [1, 2, 3, 2, 4, 2, 4]  // ArrayList

 

2、 How to define an array

Groovy There are also two ways to define arrays in , First, use. Java To create an array , The two is the use of Groovy Language to create arrays , Use as keyword

//  Define array usage as keyword 
def array = [1, 2, 1, 34] as int[]
int[] array1 = [1, 2, 3, 4] //  Use strong typing to define arrays 

 

3、 The addition, deletion and modification of the list

notes : because Groovy The operation method of array in is the same as that of list , So I will only introduce the operation of the list

3.1、List Add operation of

3.1.1、add Method

def testList = [1]
testList.add(6)

3.1.2、leftShift Method

def testList = [1]
testList.leftShift(7)

3.1.3、 Use << Operator to add elements

def testList = [1]
testList << 8

3.1.4、 Use + Operator to add elements

notes : The result is a new list

def testList = [1]
def PutList = testList + 9

3.2、List Delete operation of

3.2.1、remove, Deletes the specified element according to the index

def LIST = [6, -3, 9, 2, -7, 1, 5,5]
LIST.remove(7)

3.2.2、remove((Object) object), Delete the element specified in the list according to the incoming object

LIST.remove((Object) 6)

3.2.3、removeAt, Delete the elements in the collection according to the passed in index And remove The method is similar to

LIST.removeAt(1)

3.2.4、removeElement, The element specified in the list is deleted according to the incoming object, which is similar to method 2

LIST.removeElement(6)

3.2.5、removeAll, Remove qualified elements with closures

LIST.removeAll{return it % 2 == 0}

3.3、 Sorting operation of list  

3.3.1 With the help of Collections Class sort Sorting method

def sortList = [5, 67, -1, 46, 4, -33]
Collections.sort(sortList)
println sortList

this sort Method also has an overloaded method , You can sort according to our custom rules

Comparator mc = { a, b -> a == b ? 0 : Math.abs(a) < Math.abs(b) ? -1 : 1 } //  Use closures to encapsulate judgment conditions 
Collections.sort(sortList, mc)
println sortList

3.3.2、Groovy Medium sort Method

Groovy For us sort Method , You can sort directly

sortList.sort()
println sortList

Groovy Medium sort Methods can also be used with closures

sortList.sort { a, b -> a == b ? 0 : Math.abs(a) > Math.abs(b) ? -1 : 1 }
println sortList

String sorting , Sort by string length , Do not specify collation , Just follow the initials ASCLL Code to sort

def sortStringList = ["abc", "zfj", "z", "Lucy"]
sortStringList.sort { it -> return it.length() } 
println sortStringList

3.4、 Search list

3.4.1、find

find Method finds the first qualified element

def findList = [-3, 9, 6, 2, -7, 5]
int result = findList.find{return it % 2 == 0}

3.4.2、findAll

findAll Method to find all the elements that meet the criteria , And encapsulate it as java.util.ArrayList Go back

ArrayList result = findList.findAll{return it % 2 == 0}

3.4.3、any

any Methods are used with closures , Find the element of the condition in the list , As long as a qualified element is found, it returns true, Otherwise, return false

def result = findList.any { return it % 2 == 0 } // result The value of is boolean type 

3.4.4、every

This method and any contrary , The elements in the list must meet the conditions in the closure before returning true, Otherwise, return false

def result = findList.every { return it % 2 == 0 }

This is just an introduction to Groovy In the middle of the day , You can also use java Medium indexof Other methods , Perform list lookup , I won't introduce it here

3.4.5、 Find the minimum and maximum values in the list

min Method to find the minimum value ,max Method to find the maximum value

def min= findList.min()
def max= findList.max()

Here are two ways , You can also use closures for custom size comparison

def result = findList.min{return Math.abs(it)}

3.4.6、 Statistics query , Count the number of qualified elements ( Use with closures )

def result = findList.count { return it % 2 == 0 }

原网站

版权声明
本文为[Leisurely summer]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221317373733.html