Groovy中定义List和Map方式比较特别
List :
def list = [1, 2, 3]
删除list原始
1、使用下标
复制代码
1
2
3list = [1,2,3,4] list.remove(2) assert list == [1,2,4]
复制代码
1def list = [1, 2, 3, 4, 5] list.removeAt(2) println list // outputs [1, 2, 4, 5]
2、删除元素
复制代码
1
2
3list = [1,2,3,4] list.remove(list.indexOf(2)) assert list == [1,3,4]
复制代码
1def list = [1, 2, 3, 4, 5] list.removeAll{ it == 2} println list // outputs [1, 3, 4, 5]
复制代码
1def list = [1, 2, 3, 4, 5] list.removeAll{ it % 2 == 0} println list // outputs [1, 3, 5]
3、获取子集
复制代码
1
2
3list = [1, 2, 3, 4] newList = list.findAll { it != 2 } assert list == [1, 3, 4]
4、克隆
复制代码
1
2
3
4
5
6list1 = [1, 2, 3] println list1 def list2 = list1 //拷贝引用 浅拷贝 list2 = list1.collect() //拷贝值 深拷贝 assert list2 == [1, 2, 3]
转载于:https://www.cnblogs.com/zhengwangzw/p/9962198.html
最后
以上就是土豪柚子最近收集整理的关于Groovy 集合操作的全部内容,更多相关Groovy内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复