当前位置:网站首页>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 << 83.1.4、 Use + Operator to add elements
notes : The result is a new list
def testList = [1]
def PutList = testList + 93.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 sortListthis 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 sortList3.3.2、Groovy Medium sort Method
Groovy For us sort Method , You can sort directly
sortList.sort()
println sortListGroovy 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 sortListString 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 sortStringList3.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 }边栏推荐
- 一文彻底弄懂建造者模式(Builder)
- Transformers vit image model vector acquisition
- Dessert mall management system based on SSH framework [source code + database]
- 软件项目验收测试范围和流程,这些你都知道吗?
- Tianrun cloud is about to be listed: VC tycoon Tian Suning significantly reduces his holdings and is expected to cash out HK $260million
- Basic usage and FAQs of jasperreport report report generation tool
- D damage and safety
- Lisez ceci pour vous apprendre à jouer avec la cible de test de pénétration vulnhub - driftingblues - 5
- How to use SQL to modify in the database & delete
- 3DMAX modeling notes (I): introducing 3DMAX and creating the first model Hello World
猜你喜欢
![[untitled]](/img/ff/f23e5275683d4d7b13fcb94cc77702.png)
[untitled]

Maui uses Masa blazor component library

VR全景拍摄,打破传统宣传雁过不留痕的僵局
![[introduction to postgraduate entrance examination] analysis of postgraduate entrance examination data of Cyberspace Security Major of Beijing Jiaotong University from 2018 to 2022](/img/84/b572b3b80cc0dd1489076116cf0638.png)
[introduction to postgraduate entrance examination] analysis of postgraduate entrance examination data of Cyberspace Security Major of Beijing Jiaotong University from 2018 to 2022

看完这篇 教你玩转渗透测试靶机Vulnhub——DriftingBlues-5

Stephencovey's tips for efficient work for young people

Database employment consulting system for your help

Traffic replication in istio Service Grid

Transformers vit image model vector acquisition

看完這篇 教你玩轉滲透測試靶機Vulnhub——DriftingBlues-5
随机推荐
VR全景拍摄,打破传统宣传雁过不留痕的僵局
如何保护WordPress网站免受网络攻击?采取安全措施至关重要
Open source SPL redefines OLAP server
What does Huawei's minutes on patents say? (including Huawei's top ten inventions)
Excuse me, is Zhongyan futures reliable? Is the fund safe?
CVE-2022-22965複現
【考研攻略】北京交通大学网络空间安全专业2018-2022年考研数据分析
S7-200SMART与FANUC机器人进行Profinet通信的具体方法和步骤
Cat agile team coaching workshops - August 20
Tasks and responsibilities of the test team and basic concepts of testing
Fluentd is easy to get started. Combined with the rainbow plug-in market, log collection is faster
How to add a mask to a VR panoramic work? What is the function?
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
哈希索引设计发展史
How to solve the problem of amount accuracy in financial applications (take quorum and golang as examples)
机器学习之感知机
Error: Unable to find a match: lrzsz的解决方法
一文彻底弄懂单例模式(Singleton)
Support vector machine for machine learning
polardbx是pg还是mysql?