美文网首页
groovy(6)-list和map

groovy(6)-list和map

作者: 高斯巴 | 来源:发表于2018-11-12 14:48 被阅读0次

    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)

    }

    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} //是否有奇数

    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{

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

    相关文章

      网友评论

          本文标题:groovy(6)-list和map

          本文链接:https://www.haomeiwen.com/subject/tyqcfqtx.html