当前位置:网站首页>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 }边栏推荐
- Getting started with go web programming: validators
- 技术实践 | 场景导向的音视频通话体验优化
- client-go gin的简单整合九-Create
- Cve - 2022 - 22965 Resume
- 一文彻底弄懂建造者模式(Builder)
- “不敢去懷疑代碼,又不得不懷疑代碼”記一次網絡請求超時分析
- How to solve the problem of amount accuracy in financial applications (take quorum and golang as examples)
- 5G时代,如何打造一场令人惊叹的VR直播活动?
- 一文彻底弄懂单例模式(Singleton)
- If you want to know the stock account opening discount link, how do you know? Is it safe to open an account online?
猜你喜欢

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

Understand the quality assurance of open source software (OSS)

【考研攻略】北京交通大学网络空间安全专业2018-2022年考研数据分析

Madcap flare 2022, documentation in language or format

Maui uses Masa blazor component library

技术实践 | 场景导向的音视频通话体验优化

数据库 就业咨询系统求各位帮下忙

Rongyun: let the bank go to the "cloud" easily

Specific methods and steps of PROFINET communication between s7-200smart and Fanuc robot

加密市场进入寒冬,是“天灾”还是“人祸”?
随机推荐
Madcap flare 2022, documentation in language or format
Offline physical stores combined with VR panorama make virtual shopping more realistic
Transformers vit image model vector acquisition
Is polardbx PG or MySQL?
Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
Are you familiar with redis cache of highly paid programmers & interview questions series 114? How are redis keys designed? Are you familiar with the memory obsolescence mechanism?
transformers VIT图像模型向量获取
[introduction to postgraduate entrance examination] analysis of postgraduate entrance examination data of Cyberspace Security Major of Beijing Jiaotong University from 2018 to 2022
uniapp app 端截屏且保存到本地
程序员要不要选择软件人才外包公司?
Technology practice | scene oriented audio and video call experience Optimization
STM32F1与STM32CubeIDE编程实例-光断续传感器驱动
防火墙基础之策略部署
Policy deployment of firewall Foundation
Detailed explanation of rules and ideas for advance sale of deposit
11 method reference and constructor application
CSAPP之详解Labs
Unity prevents the BTN button from being clicked continuously
Flink status management
一文彻底弄懂单例模式(Singleton)