当前位置:网站首页>Groovy之列表操作
Groovy之列表操作
2022-06-22 13:18:00 【悠然予夏】
1、列表的定义方式
Groovy中定义列表的方式有两种,一是使用Java中的方法来创建列表,二是使用Groovy语言中的方法来创建列表
def list = new ArrayList() // java中的定义方式
// Groovy中定义列表的方式
def list = [1, 2, 3, 2, 4, 2, 4] // ArrayList
2、数组的定义方式
Groovy中数组的定义方式也有两种,一是使用Java中的方法来创建数组,二是使用Groovy语言中的方法来创建数组,使用as关键字
// 定义数组使用as关键字
def array = [1, 2, 1, 34] as int[]
int[] array1 = [1, 2, 3, 4] // 使用强类型定义数组
3、列表的增删改查找
注:因为Groovy中数组的操作方法与列表的操作方法相同,所以我只介绍列表的操作方式
3.1、List的添加操作
3.1.1、add方法
def testList = [1]
testList.add(6)3.1.2、leftShift方法
def testList = [1]
testList.leftShift(7)3.1.3、使用 <<运算符添加元素
def testList = [1]
testList << 83.1.4、使用 + 运算符添加元素
注:这种方式产生的是一个新的列表
def testList = [1]
def PutList = testList + 93.2、List的删除操作
3.2.1、remove,根据索引删除指定元素
def LIST = [6, -3, 9, 2, -7, 1, 5,5]
LIST.remove(7)3.2.2、remove((Object) object),根据传入对象删除列表中指定的元素
LIST.remove((Object) 6)3.2.3、removeAt,根据传入的索引删除集合中的元素 与remove方法类似
LIST.removeAt(1)3.2.4、removeElement,根据传入对象删除列表中指定的元素与方法二类似
LIST.removeElement(6)3.2.5、removeAll,借助闭包移除符合条件的元素
LIST.removeAll{return it % 2 == 0}3.3、列表的排序操作
3.3.1借助Collections类中的sort排序方法
def sortList = [5, 67, -1, 46, 4, -33]
Collections.sort(sortList)
println sortList此sort方法还有一个重载方法,可以按照我们自定义的规则进行排序
Comparator mc = { a, b -> a == b ? 0 : Math.abs(a) < Math.abs(b) ? -1 : 1 } // 使用闭包进行判断条件的封装
Collections.sort(sortList, mc)
println sortList3.3.2、Groovy中的sort方法
Groovy 中为我们提供的sort方法,直接就可以进行排序
sortList.sort()
println sortListGroovy中的sort方法还可以搭配闭包进行使用
sortList.sort { a, b -> a == b ? 0 : Math.abs(a) > Math.abs(b) ? -1 : 1 }
println sortList字符串的排序,根据字符串的长度进行排序,不指定排序规则,就按照首字母的ASCLL码进行排序
def sortStringList = ["abc", "zfj", "z", "Lucy"]
sortStringList.sort { it -> return it.length() }
println sortStringList3.4、列表的查找
3.4.1、find
find方法找出的是第一个符合条件的元素
def findList = [-3, 9, 6, 2, -7, 5]
int result = findList.find{return it % 2 == 0}3.4.2、findAll
findAll方法找出所有符合条件的元素,并把它封装为java.util.ArrayList进行返回
ArrayList result = findList.findAll{return it % 2 == 0}3.4.3、any
any方法搭配闭包使用,在列表中查找条件的元素,只要找到一个符合条件的元素就返回true,反之则返回false
def result = findList.any { return it % 2 == 0 } // result的值为布尔类型3.4.4、every
此方法与any相反,必须列表中的元素满足闭包中的条件才返回true,反之则返回false
def result = findList.every { return it % 2 == 0 }这里只是介绍Groovy中的用法,还可以使用java中的indexof等方法,进行列表查找,这里就不介绍了
3.4.5、查找列表中的最小值与最大值
min方法查找最小值,max方法查找最大值
def min= findList.min()
def max= findList.max()这里的两个方法,也可以搭配闭包进行自定义大小比较
def result = findList.min{return Math.abs(it)}
3.4.6、统计查询,统计符合条件的元素个数(搭配闭包使用)
def result = findList.count { return it % 2 == 0 }边栏推荐
- BSN发展联盟理事长单志广:DDC可为中国元宇宙产业发展提供底层支撑
- MadCap Flare 2022,语言或格式的文档
- S7-200SMART与FANUC机器人进行Profinet通信的具体方法和步骤
- What is the difference between Z-score and deltf/f?
- 一文彻底弄懂工厂模式(Factory)
- Nine good programming habits for 10 years
- 基于SSH框架甜品商城管理系统【源码+数据库】
- Cosmos、Polkadot
- 金融应用如何解决金额精度问题(以Quorum、golang为例)
- Chip silicon and streaming technology
猜你喜欢

How to protect WordPress websites from cyber attacks? It is important to take safety measures

5G时代,如何打造一场令人惊叹的VR直播活动?

Technology practice | scene oriented audio and video call experience Optimization

Go Web 编程入门:验证器

Database employment consulting system for your help

Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!

STM32F1与STM32CubeIDE编程实例-光断续传感器驱动

Is the encryption market a "natural disaster" or a "man-made disaster" in the cold winter?

VR全景拍摄,打破传统宣传雁过不留痕的僵局

Chengdu test equipment development_ Array introduction of C language for single chip microcomputer
随机推荐
Messiari annual report-2021
Oceanbase database helps the ideal automobile intelligent production line to realize automatic recovery within 30 seconds
机器学习之感知机
基于SSM框架实现的甜品饮品店前后台管理系统甜品商城蛋糕店【源码+数据库】
How to use SQL to modify in the database & delete
How many days are there between the two timestamps of PHP
After reading this article, I will teach you to play with the penetration test target vulnhub - drivetingblues-5
Unity sub thread calls UI of main thread
定金预售的规则思路详解
How to implement interface exception scenario testing? Exploration of test methods and implementation of test tools
Lisez ceci pour vous apprendre à jouer avec la cible de test de pénétration vulnhub - driftingblues - 5
Offline physical stores combined with VR panorama make virtual shopping more realistic
unity的富文本Text的Color设置颜色全透明
Should programmers choose software talent outsourcing companies?
金融应用如何解决金额精度问题(以Quorum、golang为例)
请问,中衍期货是不是靠谱的?资金安全吗?
transformers VIT图像模型向量获取
How MySQL enables multiple auto incrementing columns in a table
天润云上市在即:VC大佬田溯宁大幅减持,预计将套现2.6亿港元
A simple scientific research secret