Groovy入门--集合
2017-01-19 14:34
197 查看
集合概述
Groovy直接在语言内使用集合。
1.不需要导入专门的类,也不需要初始化对象。
2.集合是语言本身的本地成员
每个Groovy集合都是java.util.Collection 或 java.util.Map 的事例。
List,String,StringBuffer,Range,Map,File,Matcher都使用统一的size()方法获取长度
列表一
列表二
列表三
列表List的方法一
列表List的方法二(有些方法会修改原列表,有些方法不修改原列表而产生新的列表)
映射Map一
映射Map二
映射Map3
范围一
范围二
范围三
范围Range的方法
Groovy直接在语言内使用集合。
1.不需要导入专门的类,也不需要初始化对象。
2.集合是语言本身的本地成员
每个Groovy集合都是java.util.Collection 或 java.util.Map 的事例。
List,String,StringBuffer,Range,Map,File,Matcher都使用统一的size()方法获取长度
列表一
def toys = [['a','001'],[2,'002'],['c','003']] println toys.class //输出:class java.util.ArrayList assert toys instanceof Collection //无断言错误 println toys[1] //索引从0开始,输出:[2,"002"] println toys.get(1) //同上输出:[2,"002"] println toys[-2] //同上输出:[2,"002"] println toys[1..<2] //输出:[[2,"002"]] toys[2] = [3,'003'] //修改第三个元素 println toys[-1] //输出:[3,"003"] toys.putAt(2,[33,'333']) println toys[-1] //输出:[33,"333"]
列表二
toys << [4,'004'] //追加元素 println toys[-1] //输出:[4,"004"] toys1 = [1,2,3] //连接列表 toys2 = toy1 + [4,5] println toys2 //输出:[1,2,3,4,5] toys3 = toys2 - [5] //列表中删除元素 println toys3 //输出:[1,2,3,4]
列表三
def toy8 = [] toy8 << '11' toy8 << '22' toy8 << '33' toy8 << '44' println toy8 //输出:["11","22","33","44"] toy8 << [8,'008'] println toy8 //输出:["11","22","33","44",[8,"008"]]
列表List的方法一
def list = [1,2,3,4] list.add(5) //[1,2,3,4,5] list.add(2,11) //[1,2,11,3,4,5] list.addAll([6,7]) //[1,2,11,3,4,5,6,7] println list.contains(11) //true println list.containsAll([11,4]) //true println list.indexOf(11) //2 list.remove(2); println list //[1,2,3,4,5,6,7] list.removeAll([5,6,7]) println list //[1,2,3,4] list.clear();println list //[]
列表List的方法二(有些方法会修改原列表,有些方法不修改原列表而产生新的列表)
def fList = [1,2,3,[4,5]] println fList.flatten() //[1,2,3,4,5],展开后返回新列表 println fList.intersect([3,4,5]) //[3],求交集,返回新列表 println fList.pop() //[4,5],删除列表最后元素 println fList.reverse() //[3.2.1],反转列表返回新列表 println fList.sort() //[1,2,3] def gList = [1,1,2,3,4,3] println gList.count(3) //输出:2 有两个3
映射Map一
def bookMap = [:] //定义空map println bookMap.getClass() //输出:class java.util.LinkedHashMap assert bookMap.size() == 0 //无断言错误 def toyMap = [1:'toy1',2:'toy2'] assert toyMap.containsValue('toy1') //无断言错误 assert toyMap.containsKey 4000 (1) //无断言错误
映射Map二
println toyMap //输出整个Map,[1,"toy1",2:“toy2”] println toyMap[2] //toy2 println toyMap.get(1) //toy1 toyMap.each( println toy.key + ':' + toy.value }//1:toy1 2:toy2 toyMap.each( println it.key + ':' + it.value }//使用默认闭包参数it遍历map,1:toy1 2:toy2
映射Map3
toyMap.put(3,'toy3') //往map中加入元素 println toyMap //[1:"toy1",2:"toy2",3:"toy3"] toyMap.put(3,'toy333') //键已存在,put就变成了修改值 println toyMap //[1:"toy1",2:"toy2",3:"toy333"] toyMap.remove(3) //删除map中的元素,参数是键 println toyMap //输出:[1:"toy1",2:"toy2"] println toyMap.size() //获取Map大小,输出:2 println toyMap.keySet() //获取Map中的key,输出:[1,2] println toyMap.values() //输出:["toy1","toy2"] println toyMap.values().asList //转换成ArrayList println toyMap.values().asList.class //输出:class java.util.ArrayList
范围一
def aRange = 1..<5 println aRange //输出:[1,2,3,4] println aRange.class //输出:class groovy.lang.IntRange println aRange.getClass().getName() //输出:groovy.lang.IntRange assert aRange instanceof List //无断言错误 //(1..<5)范围是IntRange的对象,是特殊的List
范围二
def bRange = 'a'..<'e' println bRange //输出:["a","b","c","d"] println bRange.class //输出:class groovy.lang.ObjectRange assert bRange instanceof List //无断言错误 //('a'..<'e') 是ObjectRange的对象,是特殊的List
范围三
def cRange = 5..1 println cRange //输出:[5,4,3,2,1] def dRange = 'e'..<'a' //输出:["e","d","c","b"] //范围可以使用倒序
范围Range的方法
println eRange.size() //输出:10 println eRange.contains(5) //输出:true println eRange.get(8) //输出:9 println eRange.getFrom() //输出:1 println eRange.getTo() //输出:10 println eRange.isReverse() //输出:false println eRange.subList(3,6) //输出:[4,5,6]
相关文章推荐
- groovy入门3 集合
- Groovy入门(四)——集合Map
- Groovy入门(二)——集合类型-Range
- Groovy入门(三)——集合类型List
- Groovy轻松入门——搭建Groovy开发环境
- Groovy Tip 6 集合类的简化操作
- Groovy轻松入门——搭建Groovy开发环境
- JS入门代码集合第1/4页
- Groovy入门(2)
- Groovy基础入门
- Groovy轻松入门——通过与Java的比较,迅速掌握Groovy(3)
- FLEX入门篇--------各种dataProvider大集合
- JavaScript基本入门语法集合第1/3页
- Groovy轻松入门——Grails实战基础篇(1)
- Groovy轻松入门——Grails实战基础篇(4)
- Groovy轻松入门——通过与Java的比较,迅速掌握Groovy(2)
- c#经典入门学习笔记-定义集合
- Groovy语言入门(1)
- JavaScript基本入门语法集合第1/3页
- Groovy轻松入门——Grails实战基础篇(2)