1. 程式人生 > >Groovy入門--集合

Groovy入門--集合

集合概述
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(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]