1.List
//List定义使用
//def list = new ArrayList()
def list = [1, 2, 3, 4, 5]
println list.class //class java.util.ArrayList
println list.size()
//定义数组
def array = [1,2,3,4,5] as int[]
//1.添加
list.add(6) //[1, 2, 3, 4, 5, 6]
list<<2 //[1, 2, 3, 4, 5, 6, 2]
println list
def plusList = list+5 //[1, 2, 3, 4, 5, 6, 2, 5]
plusList.add(3,9) //[1, 2, 3, 9, 4, 5, 6, 2, 5]
println plusList
//2。删除
list.remove(2) //删除下标位置的
list.remove((Object) 2) //删除2这个对象
list.removeElement(5) //删除5这个对象
list.removeAll {
return it % 2 == 0
}
println list-[2,4] //删除2,4
//3.查找
def findList = [5, -2, 1, 4, 3]
//查找满足条件的第一个数据
int result = findList.find {
return it % 2 == 0
}
println result //-2
//查找所有满足条件的
def result2 = findList.findAll {
return it % 2 == 0
}
println result2 //[-2, 4]
//查找是否有满足条件的数据
def result3 = findList.any {
return it % 2 == 0
}
println result3 //true
//查找是否全部满足条件
def result4 = findList.every {
return it % 2 == 0
}
println result4 //false
//查找最大值与最小值
def result5 = findList.min {
return Math.abs(it) //1
}
println result5
def result6 = findList.max {
return Math.abs(it)
}
println result6 //5
//统计
int result7 = findList.count {
return it > 0
}
println result7 //4
//4.排序
def sortList=[5,-2,1,4,3]
sortList.sort({a,b ->
a == b ? 0 : Math.abs(a)>Math.abs(b) ? 1 :-1
})
println sortList //[1, -2, 3, 4, 5]
//对象排序
def sortStringList=['aaaaa','bbbb','c','ddd','ee']
sortStringList.sort({it ->
return it.size()
})
println sortStringList //[c, ee, ddd, bbbb, aaaaa]
2.Map
//定义与读取
def colors=[red:'ff0000',green:'00ff00',blue:'0000ff']
println colors['red']
println colors.red
//如果使用colors.class 会把class当成一个键
//class java.util.LinkedHashMap
println colors.getClass()
//1.添加普通对象
colors.yellow='ffff00'
println colors
//添加集合对象
colors.map = [key1:1,key2:2]
//[red:ff0000, green:00ff00, yellow:ffff00, map:[key1:1, key2:2]]
println colors.toMapString()
//2.遍历map
def teachers = [
1: [number: '001', name: 'jett'],
4: [number: '004', name: 'alven'],
3: [number: '003', name: 'lance'],
8: [number: '008', name: 'jett']
]
//用键值对的方式
teachers.each {def key,def value ->
println "key=${key}---value=${value}"
}
//用entry对象的方式
teachers.each {def teacher ->
println "key=${teacher.key}---value=${teacher.value}"
}
//带索引的方式
teachers.eachWithIndex{ def teacher,int index->
println "index=${index}---key=${teacher.key}---value=${teacher.value}"
}
teachers.eachWithIndex{ def key,def value,int index->
println "index=${index}---key=${key}---value=${value}"
}
//3.map的查找
def entry=teachers.find{def teacher ->
return teacher.value.name=='jett'
}
println entry //1={number=001, name=jett}
def entry=teachers.findAll{def teacher ->
return teacher.value.name=='jett'
}
println entry//[1:[number:001, name:jett], 8:[number:008, name:jett]]
def count=teachers.count{def teacher ->
return teacher.value.name=='jett'
}
println count //2
//实现嵌套查询
def number=teachers.findAll{def teacher->
return teacher.value.name=='jett'
}.collect(){
return it.value.number
}
println number.toListString() //[001, 008]
//实现分组查询
def group=teachers.groupBy {def teacher ->
return teacher.value.name=='jett' ? 'group1' : 'group2'
}
//[group1:[1:[number:001, name:jett], 8:[number:008, name:jett]],
//group2:[4:[number:004, name:alven], 3:[number:003, name:lance]]]
println group.toMapString()
//4.排序 注意:map会返回一个新的map list是在原来的list中进行排序
def sort=teachers.sort{def t1,def t2 ->
return t1.key > t2.key ? 1 : -1
}
//[1:[number:001, name:jett], 3:[number:003, name:lance], 4:[number:004, name:alven], 8:[number:008, name:jett]]
println sort.toMapString()
3.Range
//定义
//Range相当于一个轻量级的List
def range=1..10
println range[0]
println range.contains(8)
println range.from //起点
println range.to //终点
//遍历
//range.each {
// println it
//}
//for(i in range){
// println i
//}
def getGrade(Number score){
def result
switch(score){
case 0..<60:
result='不及格'
break;
case 60..100:
result='及格'
break;
default:
result='输入异常'
}
// return result
}
println getGrade(50)
println getGrade(80)
网友评论