Groovy笔记(3)_集合
集合概述
1、集合应用很广泛
2、Groovy直接在语言内使用集合
- 不需要导入专门的类,也不需要初始化对象
- 集合石语言本身的本地成员
3、每个Groovy集合都是java.util.Colleciton或java.util.Map的实例
4、List,String,StringBuffer,Range,Map,File,Matcher都是用同一的size()方法获取长度.
如:StringBUffer x = new StringBuffer("ssss");
print x.size() //输出4
列表List一
def toys =[["a","001"] , [2,"002"] , ["c", "003"]]
- println toys.class //输出class java.util.ArrayList
- assert toys instanceof Collection
- print toys[1] //输出[2,"002"]
- print toys.get(1) //同上
- print toys[-2] //同上
- print toys[1..<2] //同上
- toys[2] = [3,"003"] //修改第3个元素
- print toys[-1] //输出: [3,"003"]
- toys.putAt(2,[33,"333"])
- print toys[-1] //输出[33,"333"]
列表List二
1、toys<<[4,"004"] //追加元素
println toys[-1] //输出:[4,"004"]
2、toys1 = [1,2,3] //连接链表
toys2 = toys1 + [4,5]
println toys2 // 输出:[1,2,3,4,5]
3、toys3 = toys2 - [5]//列表中删除元素
println toys3 //输出:[1,2,3,4]
列表List三
def toy6 =[]
- toy6<<"11"
- toy6<<"22"
- toy6<<"33"
- toy6<<"44"
- println toy6 //输出["11","22","33","44"]
- toy6<<[8,"008"]
- println toy6 //输出["11","22","33","44",[8,"008"]] 列表中元素无需一样类型
列表List的方法一
1、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.remveAll([5,6,7])
- println list //[1,2,3,4]
- list.cleat();println list //[]
列表List的方法二
1、有些方法会修改原列表,有些方法不修改原列表而只是产生新的列表。
def fList =[1,2,3[4,5]]
- println fList.flatten() //[1,2,3,4,5]展开后返回新列表
- println fList.inersect([3,4,5]) //[3],求交集,返回新列表
- println fList.pop() //[4,5] 删除列表最后元素
- println fList.reverse() //[3,2,1],反转列表返回新列表
- println fLIst.sort() //[1,2,3]
2、计算元素
def gList = [1,1,2,3,4,3]
println gList.count(3) //输出:2 有两个3
映射Map一
1、def bookMap = [:] //定义空map
println bookMap.getClass() // 输出class java.util.LinkedHashMap
assert bookMap.size() == 0
2、def toyMap = [1:"toy1",2:"toy2"]
assert toyMap.containsValue("toy1")
assert toyMap.containsKey(1)
映射Map二
1、println toyMap //输出整个map, [1:"toy1",2:"toy2"]
println toyMap[2] // toy2
println toyMap.get(1) // toy1
2、toyMap.each{toy -> println toy.key + ":" + toy.value} // 1:toy1 2:toy2(换行输出) ,toy是指定的闭包参数
toyMao.each{println it.key + ":" + it.value} //输出同上,it是默认闭包参数
映射MAP三
- toyMap.put(3,"toy3") //往map中加入元素
- println toyMap //[1:"toy1",2:"toy2",3:"toy3"]
- topMap.put(3,"toy333") //键已存在,put就变成了修改值
- println toyMap //[1:"toy1",2:"toy2",3:"toy333"]
- toyMap.remove(3) //删除map中的元素,参数是键
- println toyMap.size() //获取Map大小 2
- println toMap.keySet() //获取Map中的key,输出[1,2]
- println toMap.values() //输出:["toy1","toy2"]
- println toMap.values().asList() //转换为ArrayList
- println toMap.values().asList().class //class java.util.ArrayList
范围一
1、def aRange = 1..<5
println aRange //输出[1,2,3,4]
println aRange.class //class groovy.lange.IntRange
assert aRange instanceof List
(1..<5)范围是IntRange的对象,是特殊的List
范围二
1、def bRange = "a"..<"e"
println bRange // 输出: ["a","b","c","d"]
println bRange.class //输出groovy.lang.ObjectRange
assert bRange instanceof List
("a"..<"e")是 ObjectRange的对象,是特殊的List
范围三
倒序
1、def cRange = 5..1
println cRange //[5,4,3,2,1]
2、def dRange = "e".."a" //["e","d","c","b"]
范围Range的方法
def eRange =1..10
- println eRange.size() //10
- println eRange.contains(5) //true
- println eRange.get(8) //9
- println eRange.getFrom() //1
- println eRange.getTo() //10
- prinltn eRange.isReverse() //false
- prinltn eRange.subList(3,6)//[4,5,6]
阅读更多
- 上一篇: Scala 自学笔记
- 下一篇:没有了