groovy之list和map方法
来源:互联网 发布:看图软件apk 编辑:程序博客网 时间:2024/05/19 13:44
java作为一门强大的系统编程语言,已经在世界各地广泛地应用.groovy是增强java平台的唯一脚本语言,它的语法更
加简洁,能快速开发.
List:
定义list:def list = []
list = [1,2,3,4,5]
list操作:
def list = [1,2,3,4,5]
list[1] //Result: 2
list[-2] //Result: 4
list[1..3] //Result: [2, 3, 4]
list[1..<3] //Result: [2, 3]
list + [6,7] //Result: [1, 2, 3, 4, 5, 6, 7]
list - [4,5,6] //Result: [1, 2, 3]
list << 6 //Result: [1, 2, 3, 4, 5, 6]
list << [6,7] //Result: [1, 2, 3, 4, 5, 6, [6, 7]]
list方法:
[2,5].add(7) //Result: true; list = [2, 5, 7]
[2,5].add(1,9) //list = [2, 7, 5]
[2,5].add([7,9]) //Result: [2, 5, [7, 9]]
[2, 5, [7, 9]].flatten() //Result: [2, 5, 7, 9];克隆并解开下层list
[2,5].get(1) //Result: 5
[2,5].size() //Result: 2
[2,5].isEmpty() //Result: false
[2,5].getAt(1) //Result: 5
[2,5,7].getAt(1..2) //Result: [5, 7]
[2,5,7].getAt(-1) //Result: 7;get()不支持负数参数,getAt()支持
[2,5,7].getAt([1,2]) //Result: [5, 7]
[2,5,7].intersect([5,9,2]) //Result: [5, 2];交集
[2,5,7].pop() //Result: 7
[2,5,7].plus([3,6]) //Result: [2, 5, 7, 3, 6]
[2,5,7,2].minus(2) //Result: [5, 7]
[2,5,7].remove(1) //Result: 5; list = [2, 7]
[2,7,5].reverse() //Result: [5, 7, 2]
[2,7,5].sort() //Result: [2, 5, 7]
Map:
定义Map:def map = [:]
map = ['name':'Bruce', 'age':27]
键被解释成字符串:
def x = 3
def y = 5
def map = [x:y, y:x] //Result: ["x":5, "y":3]
如果要把值作为键,像下面这样:
def city = 'shanghai'
map."${city}" = 'china'
map.shanghai //Result: "china"
map操作:
def map = [3:56, 'name':'Bruce']
def a = 'name'
map.name //Result: "Bruce"
map['name'] //Result: "Bruce"
map[a] //Result: "Bruce"
map[3] //Result: 56
以下访问是错误的,会抛出异常
map[name]
map.3
map方法:
def map = ['name':'Bruce', 'age':27]
map.containsKey('name') //Result: true
map.get('name') //Result: "Bruce"
map.get('weight', '60kg') //Result: "60kg";会把key:value加进去
map.getAt('age') //Result: 27
map.keySet() //Result: [name, age, weight]
map.put('height', '175') //Result: ["name":"Bruce", "age":27, "weight":"60kg", "height":"175"]
map.values().asList() //Result: ["Bruce", 27, "60kg", "175"]
map.size() //Result: 4
下列方法可以应用于范围、List和Map(inject和reverseEach方法只适合List和范围)
each void each(Closure clos)迭代集合中每个元素。
find List find(Closure clos)返回集合中第一个符合条件的元素。
findAll List findAll(Closure clos)返回集合中所有符合条件的元素。
collect List collect(Closure clos)返回计算后的列表。
collect List collect(Collection col, Closure clos)返回计算后的列表,同时把返回值保存到col集合里。
any boolean any(Closure clos)集合中有一个符合条件即返回true,否则返回false。
every boolean every(Closure clos)集合中所有都符合条件即返回true,否则返回false。
findIndexOf int findIndexOf(Closure clos)返回第一个符合条件元素在集合中的索引值(从0开始计算)。
findLastIndexOf int findLastIndexOf(Closure clos)返回最后一个符合条件元素在集合中的索引值(从0开始计算)。
inject Object inject(Object value, Closure clos)返回调用列表和参数的计算值。
[1,2,3,4].inject(5) {x,y->
x + y
}
//Result: 15
reverseEach void reverseEach(Closure clos)反响迭代集合中每个元素。
[1,2,3,4].reverseEach {x->
print x + '-'
}
//4-3-2-1-
sort List sort(Closure clos)按照闭包的返回条件排序。
加简洁,能快速开发.
List:
定义list:def list = []
list = [1,2,3,4,5]
list操作:
def list = [1,2,3,4,5]
list[1] //Result: 2
list[-2] //Result: 4
list[1..3] //Result: [2, 3, 4]
list[1..<3] //Result: [2, 3]
list + [6,7] //Result: [1, 2, 3, 4, 5, 6, 7]
list - [4,5,6] //Result: [1, 2, 3]
list << 6 //Result: [1, 2, 3, 4, 5, 6]
list << [6,7] //Result: [1, 2, 3, 4, 5, 6, [6, 7]]
list方法:
[2,5].add(7) //Result: true; list = [2, 5, 7]
[2,5].add(1,9) //list = [2, 7, 5]
[2,5].add([7,9]) //Result: [2, 5, [7, 9]]
[2, 5, [7, 9]].flatten() //Result: [2, 5, 7, 9];克隆并解开下层list
[2,5].get(1) //Result: 5
[2,5].size() //Result: 2
[2,5].isEmpty() //Result: false
[2,5].getAt(1) //Result: 5
[2,5,7].getAt(1..2) //Result: [5, 7]
[2,5,7].getAt(-1) //Result: 7;get()不支持负数参数,getAt()支持
[2,5,7].getAt([1,2]) //Result: [5, 7]
[2,5,7].intersect([5,9,2]) //Result: [5, 2];交集
[2,5,7].pop() //Result: 7
[2,5,7].plus([3,6]) //Result: [2, 5, 7, 3, 6]
[2,5,7,2].minus(2) //Result: [5, 7]
[2,5,7].remove(1) //Result: 5; list = [2, 7]
[2,7,5].reverse() //Result: [5, 7, 2]
[2,7,5].sort() //Result: [2, 5, 7]
Map:
定义Map:def map = [:]
map = ['name':'Bruce', 'age':27]
键被解释成字符串:
def x = 3
def y = 5
def map = [x:y, y:x] //Result: ["x":5, "y":3]
如果要把值作为键,像下面这样:
def city = 'shanghai'
map."${city}" = 'china'
map.shanghai //Result: "china"
map操作:
def map = [3:56, 'name':'Bruce']
def a = 'name'
map.name //Result: "Bruce"
map['name'] //Result: "Bruce"
map[a] //Result: "Bruce"
map[3] //Result: 56
以下访问是错误的,会抛出异常
map[name]
map.3
map方法:
def map = ['name':'Bruce', 'age':27]
map.containsKey('name') //Result: true
map.get('name') //Result: "Bruce"
map.get('weight', '60kg') //Result: "60kg";会把key:value加进去
map.getAt('age') //Result: 27
map.keySet() //Result: [name, age, weight]
map.put('height', '175') //Result: ["name":"Bruce", "age":27, "weight":"60kg", "height":"175"]
map.values().asList() //Result: ["Bruce", 27, "60kg", "175"]
map.size() //Result: 4
下列方法可以应用于范围、List和Map(inject和reverseEach方法只适合List和范围)
each void each(Closure clos)迭代集合中每个元素。
find List find(Closure clos)返回集合中第一个符合条件的元素。
findAll List findAll(Closure clos)返回集合中所有符合条件的元素。
collect List collect(Closure clos)返回计算后的列表。
collect List collect(Collection col, Closure clos)返回计算后的列表,同时把返回值保存到col集合里。
any boolean any(Closure clos)集合中有一个符合条件即返回true,否则返回false。
every boolean every(Closure clos)集合中所有都符合条件即返回true,否则返回false。
findIndexOf int findIndexOf(Closure clos)返回第一个符合条件元素在集合中的索引值(从0开始计算)。
findLastIndexOf int findLastIndexOf(Closure clos)返回最后一个符合条件元素在集合中的索引值(从0开始计算)。
inject Object inject(Object value, Closure clos)返回调用列表和参数的计算值。
[1,2,3,4].inject(5) {x,y->
x + y
}
//Result: 15
reverseEach void reverseEach(Closure clos)反响迭代集合中每个元素。
[1,2,3,4].reverseEach {x->
print x + '-'
}
//4-3-2-1-
sort List sort(Closure clos)按照闭包的返回条件排序。
阅读全文
0 0
- groovy之list和map方法
- Groovy的list和map
- groovy 字符串 数字 list map
- 【Groovy】List对象的transpose和inject方法的使用
- groovy 循环List<Map<String, Object>>
- Groovy探索之Gpath和List的结合 更加Groovy风格的代码
- 图书馆借阅是否借阅小案例(groovy中list和map的使用)
- 遍历Map和遍历List方法
- list和map集合遍历方法
- velocity 显示List和Map方法
- velocity 显示List和Map方法
- List、Set和Map的遍历方法
- java8之list和map集合转换
- Groovy探索之MOP 一 invokeMethod和methodMissing方法
- Groovy探索之Map与DSL
- groovy Map
- Groovy map和String 互相转化
- Groovy探索之invokeMethod方法
- springMVC框架下返回json格式的对象,list,map
- websocket实现android消息推送
- RevitAPI: 注意Revit 2016事件注册和注销行为的改变
- MXNet 多rec参与训练
- MongoDb 将 BsonDocument 集合 转换为强类型 List
- groovy之list和map方法
- 小程序点击获取循环列表中的内容
- 餐馆与UX中的用户引导
- CaffeOnSpark 安装笔记 可以正常运行
- the solve of volume problem
- 保留小数点后两位(必须是数字)
- sublime text3快捷键设置为eclipse差不多
- merge and rebase
- Java Map释放内存置null以及调用clear()的区别