当前位置:网站首页>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 }边栏推荐
- Is the encryption market a "natural disaster" or a "man-made disaster" in the cold winter?
- Instanceinforeplicator class of Eureka (service registration auxiliary class)
- 基于SSH框架甜品商城管理系统【源码+数据库】
- 【Pr】基础流程
- How MySQL enables multiple auto incrementing columns in a table
- Stephencovey's tips for efficient work for young people
- Chengdu test equipment development_ Array introduction of C language for single chip microcomputer
- In 5g era, how to create an amazing live VR activity?
- 什么是Bout?
- Should programmers choose software talent outsourcing companies?
猜你喜欢

How to add a mask to a VR panoramic work? What is the function?

防火墙基础之策略部署
![Dessert mall management system based on SSH framework [source code + database]](/img/6d/6d8ad081de9b0c0ab8c2f8aba1dcf0.png)
Dessert mall management system based on SSH framework [source code + database]

基于SSM框架实现的甜品饮品店前后台管理系统甜品商城蛋糕店【源码+数据库】

成都测试设备开发_单片机C语言之数组介绍

Technology practice | scene oriented audio and video call experience Optimization

Simple integration of client go gin IX create
![[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

Common real machine debugging plug-ins for unity commercial games

天润云上市在即:VC大佬田溯宁大幅减持,预计将套现2.6亿港元
随机推荐
Chip silicon and streaming technology
JasperReport报表生成工具的基本使用和常见问题
融云:让银行轻松上“云”
Go Web 编程入门:验证器
Cat agile team coaching workshops - August 20
快速了解常用的对称加密算法,再也不用担心面试官的刨根问底
Database employment consulting system for your help
unity防止按钮btn被连续点击
Understand the quality assurance of open source software (OSS)
D security can call the system
Front and back management system of dessert beverage store based on SSM framework dessert mall cake store [source code + database]
作为过来人,写给初入职场的程序员的一些忠告
11 方法引用和构造器应用
unity的富文本Text的Color设置颜色全透明
Support vector machine for machine learning
能让Jellyfin直接挂载阿里云盘的aliyundrive-fuse
软件项目验收测试范围和流程,这些你都知道吗?
Open source SPL redefines OLAP server
开源SPL重新定义OLAP Server
Chengdu test equipment development_ Array introduction of C language for single chip microcomputer