Groovy入门--集合

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013803499/article/details/54615809

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

范围二

扫描二维码关注公众号,回复: 4358273 查看本文章
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]

猜你喜欢

转载自blog.csdn.net/u013803499/article/details/54615809
今日推荐