groovy(6)-list和map

package variable

//list定義方式,默認(rèn)list是java的arrayList

def list=[]

def list1=[10,3,14,-4,5]

//定義list為數(shù)組

def list2=[2,3,4,2] as int [] //使用as 轉(zhuǎn)為數(shù)組

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]

//自定義排序規(guī)則

Comparator mc={a,b->

????????a==b?0:Math.abs(a)

}

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{

????????returnit%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} //是否有奇數(shù)

println(result3) //true

def result4=findlist.every{return it%2!=0 } //是否都是奇數(shù)

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 統(tǒng)計個數(shù)

//groovy中的map,默認(rèn)是java中的linkedHashMap , map中key一般使用字符串或數(shù)字,默認(rèn)是單引號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{

? ?defstudent->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.score

????????????return? 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]]

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容