Groovy 集合操作

Groovy中定义List和Map方式比较特别

List : 

def  list = [1, 2, 3]

删除list原始  

1、使用下标 

list = [1,2,3,4]
list.remove(2)
assert list == [1,2,4]
def list = [1, 2, 3, 4, 5]
list.removeAt(2)
println list // outputs [1, 2, 4, 5]

2、删除元素

list = [1,2,3,4]
list.remove(list.indexOf(2))
assert list == [1,3,4]
def list = [1, 2, 3, 4, 5]
list.removeAll{ it == 2}
println list // outputs [1, 3, 4, 5]
def list = [1, 2, 3, 4, 5]
list.removeAll{ it % 2 == 0}
println list // outputs [1, 3, 5]

3、获取子集

list = [1, 2, 3, 4]
newList = list.findAll { it != 2 }
assert list == [1, 3, 4]

4、克隆

list1 = [1, 2, 3]
println list1

def list2 = list1  //拷贝引用 浅拷贝
list2 = list1.collect() //拷贝值 深拷贝
assert list2 == [1, 2, 3]

 

posted @ 2018-11-15 10:48  安琪拉的博客(公众号)  阅读(1230)  评论(0编辑  收藏  举报