Groovy List 常用操作

本文介绍了Groovy中List的多种操作,包括克隆、遍历(each, eachWithIndex, collect)、创建新集合(multiply, collect)、查找元素(find, findAll, findIndexOf, indexOf)、判断条件(any)、求和(sum)、连接字符串(join)、聚合操作(inject, max, min)、添加删除元素(add, leftShift, +=, -=, remove)以及集合的交集、不相交判断和排序等。" 107397802,8765309,前端与后端开发者的协作艺术:沟通与平衡,"['前端开发', '后端开发', '编程', '协作', '开源']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:集合克隆

  def list1 = ['a', 'b', 'c']
        def list2 = list1.clone()

2:list遍历

a:使用each进行遍历

def list = [1, 2, 3]
        list.each {
            println "Item: $it"
        }

it是是与当前元素对应的隐式参数

遍历时候使用了${}操作符。只是省略了{}

b:使用eachWithIndex进行遍历 可以打印出集合的下标和下标对应的值

 def list = ['a', 'b', 'c']
        list.eachWithIndex { it, i -> // `it` is the current element, while `i` is the index
            println "$i: $it"
        }

' it '是当前元素,而' i '是索引

c:通过将其每个元素转换为其他元素来创建一个新列表通常也很有用。由于collect方法,这个操作(通常称为映射)是在Groovy中完成的

  def list = [1, 2, 3]
       def newList =  list.collect { it * 2 }
        println(newList)

上面是将list集合通过调用collect()把list中的值*2构成一个新的集合,构成的新集合是[2,4,6]

d:使用multiply()方法 快捷语法创建一个新集合

 def list = [1, 2, 3]
       def newList =   list*.multiply(2)
        println(newList)

e:使用collect. 参数list创建一个新的集合

 def list = [0]
        def originList = [1,2,3]
        def newList = originList.collect(list) { it * 2 }
        println(newList)

查找集合中某个值

a:使用find()方法. 找到第一个元素匹配标准

  def list = [1, 2, 3]
        println(list.find { it > 1 })

这个结果是2

b:使用findAll()   找到所有匹配的元素

def list = [1, 2, 3]
        println(list.findAll{ it > 1 })

这个是[2,3]

c:使用findIndexOf()  查找第一个元素匹配标准的索引

  def strList =  ['a', 'b', 'c', 'd', 'e']
       def index =  strList.findIndexOf {
            it in ['c', 'e', 'g']
        }
        println(index)
这index的结果为2

d:使用indexOf()  查找集合某个值对应的下标值

 def strList =  ['a', 'b', 'c', 'd', 'e']
        def index =  strList.indexOf('c')
        println(index)
如果没有找到就返回-1
e:使用every 如果条件都满足 则返回true
 def list =  [1,2,3,4]
        def isTrue = list .every { it < 5 }

判断集合中的值是否都小于5

f:使用any  只要满足一个条件就返回true

def list =  [1,2,3,4,5,6,7]
        def isTrue = list .any { it < 5 }

g:使用sum()方法 对list集合所有的值求和

 def list =  [1,2,3,4,5]
        def isTrue = list.sum()

如果list中的元素是字符串的话 就相当于使用+链接符  链接起来

 def list =  ["a","b","c"]
        def str = list.sum()
        println(str)

这结果是abc

如果是二维数组使用sum() 得到的是一维数组

   def list = [['a', 'b'], ['c', 'd']]
        def newList = list.sum()
        println(newList)

h:使用sum(index)方法 把list集合相加 并+index

 def list = [1,2,4]
        def sum =list.sum(1000)
        println(sum)

这返回的是1007  是Integer

i:使用join()方法  使集合中的元素构建成一个新的字符串

 def list = [1,2,4]
        def str =list.join(",")
        println(str)

j:使用inject(index) 方法 集合中元素相加后再加index

 def list = [1,2,4]
        def num = list.inject(93) { count, item ->
            count + item
        }
        println(num)
这结果返回100,如果inject()方法中的参数是字符串的话 那么结果就是在集合的前面
 def list = [1,2,4]
        def num = list.inject("abc-") { count, item ->
            count + item
        }
        println(num)

这结果是abc-123

 

求集合中的最大值max()

 def list = [9, 4, 2, 10, 5]
        def maxValue = list.max()
        println(maxValue)

求集合中的最小值 min()

 def list = [9, 4, 2, 10, 5]
        def minValue = list.min()
        println(minValue)

如果集合中的元素是字符串的话  那么是安装26个字母的排序

使用比较器 来求最大值 最小值

  def list = [7, 4, 9, -6, -1, 11, 2, 3, -9, 5, -13]
        Comparator mc = { a, b -> a == b ? 0 : (a < b ? -1 : 1) }
        def maxValue = list.max(mc)
        println(maxValue)

 

 

集合添加删除元素操作

a:添加一个元素

 def list = []
        list << 5
        println(list)

b:添加多个元素

 def list = []
        list << 7 << 'i' << 11
        println(list)

结果:[7, i, 11]

c: 集合中添加集合

def list = [1,2,3]
        list << ['m', 'o']
        println(list)

结果:[1, 2, 3, [m, o]] 奇怪的是它是把整个集合当作一个元素添加到原始集合中

d:添加多个元素   包含集合

  def list = [1,2,3]
        def newList =  list << 3 << [4, 5] << 6
        println(newList)

结果:[1, 2, 3, 3, [4, 5], 6]

e:使用leftShift()方法往集合中添加元素

 def list = [1,2,3]
        def newList =  list.leftShift(4)
        println(newList)

结果是:[1,2,3,4]

也可以添加集合

 def list = [1,2,3]
        def orginList = [4,5,6]
        def newList =  list.leftShift(orginList)
        println(newList)

结果:[1, 2, 3, [4, 5, 6]]

f:可以使用 + 号能创建一个新的集合

 def list = [1,2,3]
        def newList =  list+4+[5,6]
        println(newList)

结果:[1, 2, 3, 4, 5, 6]

上面的 + 号操作等同于 调用plus()

 def list = [1,2,3]
        def newList =  list.plus(4).plus([5,6])
        println(newList)

g: 使用+=操作符 往集合中添加元素

def list = [1,2,3]
        list+=4
        list+=[5,6]
        println(list)

结果:[1, 2, 3, 4, 5, 6]

h:使用flatten()方法能把多维数组变成一维数组

  def list = [1, [2, 3, [4, 5], 6], 7, [8, 9]]
       def newList = list.flatten()
        println(newList)
结果:[1, 2, 3, 4, 5, 6, 7, 8, 9]

+ 和 <<那么性能好点呢? 官网给出了文档:

It is however important that the + operator on a list is not mutating. Compared to <<, it will create a new list, which is often not what you want and can lead to performance issues

+ 操作符性能比较低 不建议使用

 

上面是添加元素。下面讲下从集合中删除元素

往集合中添加元素有+操作符 那么在集合中删除元素也就有 - 操作符

 def strList = ['a','b','c','b','b']
        def newStrList = strList-'b'
        println(newStrList)

使用-操作符 删除集合

  def strList = ['a','b','c','b','b']
        def newStrList = strList-['b','c']
        println(newStrList)

对应添加元素相比 方法是通用的 也可以使用-=删除集合中的元素

 def strList = ['a','b','c','b','b']
        def newStrList = strList -= 'b'
        println(newStrList)

当然也可以删除集合中的子集合

 

判断某个值是否在集合中

  def strList = ['a','b','c','b','b']
       def isContains = strList.contains('b')
        println(isContains)

还可以使用in 关键字

  def strList = ['a','b','c','b','b']
       def isContains = 'a' in strList
        println(isContains)

这是判断单个元素在集合中是否存在 也可以判断多个元素是否在集合中

   def strList = [1,2,3,4,5,6]
       def isContains = strList.containsAll([1,7])
        println(isContains)

如果是判断多个元素 如果多存在才返回true

因为list集合是无序的,所以我们可能在某些特殊的需求下需要计算下某个值 出现的次数

def strList = [1,2,3,4,5,6,1,2,4,2]
       def count = strList.count(2)
        println(count)

也可以对集合做一些逻辑判断后再返回,使用闭包的形式

 def strList = [1,2,3,4,5,6,1,2,4,2]
        def count = strList.count {
            it%2==0                             // count the number of elements which match the predicate
        }
        println(count)

使用intersect()方法求集合的交集

 def list1 = [1,2,3,4,5,6,1,2,4,2]
        def list2 = [7,4,2]
        def newList = list1.intersect(list2)
        println(newList)
它是一个个判断 如果没有7   就判断4。然后判断2  返回一个新的集合

使用disjoint()方法 判断集合中 不相交的

  def list1 = [1,2,3]
        def list2 = [4,5,6]
        def isDisJoint = list1.disjoint(list2)
        println(isDisJoint)

不相交返回true  相交返回false

 

最后还有一个排序,Groovy也提供了方法 方便调用

  def list = [6, 3, 9, 2, 7, 1, 5]
       list.sort()
        println(list)

 

如果一个集合想把里面的元素重复的复制几次,Groovy给我们提供了非常简单的方法

  def list = [1,2,3]
        def newList = list*3
        println(newList)

结果:[1, 2, 3, 1, 2, 3, 1, 2, 3]

上面是使用了操作符 还可以使用multiply(count)方法

   def list = [1,2,3]
        def newList = list.multiply(2)
        println(newList)
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值