为了账号安全,请及时绑定邮箱和手机立即绑定

groovy(5)list&map

标签:
Android

package variable

//list定义方式,默认list是java的arrayList
def list=[]
def list1=[10,3,14,-4,5]
//定义list为数组
def list2=[2,3,4,2] as int []//使用as 转为数组
int [] list3=[3,3,2,4,5]

//列表的排序
def sortList=[4,3,2,-6,-2,49]
Collections.sort(sortList)
println(sortList)//[-6, -2, 2, 3, 4, 49]
//自定义排序规则
Comparator mc={a,b->

a==b?0:Math.abs(a)<Math.abs(b)?-1:1

}
Collections.sort(sortList,mc)
println(sortList)//[-2, 2, 3, 4, -6, 49]

//groovy中的方便排序
list1.sort()
println(list1)//[-4, 3, 5, 10, 14]
list1.sort(mc)
println(list1)//[3, -4, 5, 10, 14]

def sortStringlist=['abc','z','adsg','adsgdsr','asddd']
sortStringlist.sort{ it->it.size() }
println(sortStringlist)//[z, abc, adsg, asddd, adsgdsr]

def findlist=[-2,3,4,1,13,-5]
int result=findlist.find{

return it%2==0

}
println(result)//-2
def result2=findlist.findAll{return it%2!=0}
println(result2)//[3, 1, 13, -5]
def result3=findlist.any {return it%2!=0}//是否有奇数
println(result3)//true
def result4=findlist.every{return it%2!=0 }//是否都是奇数
println(result4)//false
println(findlist.max{return Math.abs(it)})//13
println(findlist.min{return Math.abs(it)})//1
println(findlist.count{return it >=4})//2 统计个数

//groovy中的map,默认是java中的linkedHashMap , map中key一般使用字符串或数字,默认是单引号String
def colors=[red:'ff000000',green:'00ff2334',blue:'00ff343233']
//索引,同样也可以使用java 的get 方法
println(colors.blue)//00ff343233
println(colors['blue'])//00ff343233
colors.yellow='00ff3334'
println(colors.toMapString())//[red:ff000000, green:00ff2334, blue:00ff343233, yellow:00ff3334]
colors.complex=[2:2,3:2]
println(colors.toMapString())//[red:ff000000, green:00ff2334, blue:00ff343233, yellow:00ff3334, complex:[2:2, 3:2]]
println(colors.getClass())//class java.util.LinkedHashMap
//使用其他map
def colors2=[red:'wewew',green:'adss'] as HashMap
HashMap color3=[red:'sdsese',blue:'sdses']
//map常用操作
def students=[

    1:[number:'001',name:'boj',score:442,sex:'male'],    2:[number:'002',name:'boaj',score:444,sex:'fmale'],    3:[number:'003',name:'bobj',score:445,sex:'male'],    4:[number:'004',name:'bocj',score:442,sex:'fmale'],

]
//遍历
students.each{

def student ->println("dddd::::student::key=${student.key}:::sudent:::value=${student.value}")

}
//带索引的遍历
students.eachWithIndex{ def student , int i ->

println("cccc:::index:::${i}::::key=${student.key}::::value=${student.value}")

}
//key ,value 索引,出来遍历entry,也可以直接遍历索引,
students.each {

key,value->println("aaaa::::student::key=${key}:::sudent:::value=${value}")

}
//遍历带索引,key,value
students.eachWithIndex{ key ,value, int i -> println("bbbb::index:::${i}::::key=${key}::::value=${value}")}
//find方法
def entry=students.find {def studetn->return studetn.value.score>442};
println(entry)//2={number=002, name=boaj, score=444, sex=fmale}
def entrys=students.findAll {return it.value.score>442}
println(entrys)//[2:[number:002, name:boaj, score:444, sex:fmale], 3:[number:003, name:bobj, score:445, sex:male]]
def counr=students.count {return it.value.score>442&&it.value.sex=='fmale'}
println(counr)
def names=students.findAll {return it.value.score>442}.collect {return it.value.name}//添加收集的条件
println(names)//[boaj, bobj]
//分组,类似sql语句
def group=students.groupBy {return it.value.score>442?'及格':'不及格'}
println(group.toMapString())
//[不及格:[1:[number:001, name:boj, score:442, sex:male], 4:[number:004, name:bocj, score:442, sex:fmale]], 及格:[2:[number:002, name:boaj, score:444, sex:fmale], 3:[number:003, name:bobj, score:445, sex:male]]]

//map排序
def sort=students.sort{def s1,def s2->

Number score1=s1.value.score
Number score2=s2.value.scorereturn score1==score2?0:score1>score2?1:-1

}
println(sort.toMapString())
//[1:[number:001, name:boj, score:442, sex:male], 4:[number:004, name:bocj, score:442, sex:fmale], 2:[number:002, name:boaj, score:444, sex:fmale], 3:[number:003, name:bobj, score:445, sex:male]]

原文链接:http://www.apkbus.com/blog-953329-77636.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 1
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消