美文网首页
Groovy:集合

Groovy:集合

作者: lv_mock | 来源:发表于2020-03-18 21:31 被阅读0次

    集合

    List

    定义一个集合
    //list
    def list = []
    println list.class  //class java.util.ArrayList
    
    def list2  = [] as LinkedList
    println list2.class //class java.util.LinkedList
    
    基本使用
    def list = ['A','B','C']
    println list[1]  //B
    println list[-1]  //C
    println list[3]   //null 没有报错
    println list[-4]  //报错 java.lang.ArrayIndexOutOfBoundsException: Negative array index [-4] too large for 
                      //array size 3
    
    def list = [1,2,3]
    def list2 = list[0..1]
    list2[0] = 100
    println list //[1, 2, 3]
    println list2 //[100, 2]
    
    迭代
    def list = [1,2,3]
    for(i in list) {
        print i+ " "
    }
    //输出 1 2 3
    
    def list = [1,2,3]
    def list2 = list.each {
        print it + " "
    }
    list2[0] = 100
    println list //[100, 2, 3]
    println list2 //[100, 2, 3]
    
    def list = [1,2,3]
    def list2 = list.collect {
        it *= 2
    }
    println list //[1, 2, 3]
    println list2 //[2, 4, 6]
    

    collect是重新创建了一个集合,并把collect闭包中的返回值放入数组中;each只是遍历闭包中的方法,返回的还是原来的集合。

    def list = ['Java','Groovy','Python']
    list.eachWithIndex{
        String entry, int i ->
            println entry + '--' + i
    }
    //输出
    //Java--0
    //Groovy--1
    //Python--2
    list.reverseEach {
        print it + " " //Python Groovy Java 
    }
    
    其他一些方法
    def list = ['Java','Groovy','Python']
    println list.find{ //Java
       it == "Java" //==就是java中的equals ; is就是java中的==
    }
    println list.find{ //null
        it == "Java2" //
    }
    
    def list = ['Java','Groovy','Python']
    println list.find { //Java
        true
    }
    println list.find{ //Groovy
        it.contains 'o' 
    }
    println list.findAll { //[Groovy, Python]
        it.contains('o')
    }
    
    def list = ['Java','Groovy','Python',"Java"]
    def list2 = list + "C++"
    def list3 = list - "Java"
    println list  //[Java, Groovy, Python, Java]
    println list2  //[Java, Groovy, Python, Java, C++]
    println list3  //[Groovy, Python]
    
    def list = ['Java','Groovy','Python']
    def list2 = list.add("C++")
    println list  //[Java, Groovy, Python, C++]
    println list2  //true 返回插入结果
    
    def list = ['Java','Groovy','Python']
    def list2 = list<<'C++'
    println list  //[Java, Groovy, Python, C++]
    println list2  //[Java, Groovy, Python, C++]
    
    def list = ['Java','Groovy','Python']
    def list2 = list << ['C++','JavaScript']
    println list //[Java, Groovy, Python, [C++, JavaScript]]
    println list2 //[Java, Groovy, Python, [C++, JavaScript]]
    println list.flatten() //[Java, Groovy, Python, C++, JavaScript]
    
    //展开操作符
    def list = ['Java','Groovy','Python']
    def list2 = list*.toUpperCase()
    println list //[Java, Groovy, Python]
    println list2 //[JAVA, GROOVY, PYTHON]
    
    def list = ['Java1','Groovy1','Python1']
    println list.any{ //true
        it == 'Java1'
    }
    println list.every{  //true
        it =~'1' //=~ contains
    }
    

    "+" 操作符创建新的对象;"<<" 和 add不创建新对象

    Map

    def map = [a:'apple','B':'banana',c:"cherry"] // key可以不带引号
    println map.getClass() //class java.util.LinkedHashMap
    println map.a //apple
    println map['a'] //apple
    println map.'a' //apple
    map.each {
        key,value->
            println "${key} - ${value}"
            println key + "--" + value
    }
    //输出
    //a - apple
    //a--apple
    //B - banana
    //B--banana
    //c - cherry
    //c--cherry
    println map.find { //a=apple
        key,value->
            value =~ 'a'
    }
    
    def map = [a:'apple','B':'banana',c:"cherry"]
    def map2 = map + ['l':'lemon','m':'mango']
    println map //[a:apple, B:banana, c:cherry]
    println map2 //[a:apple, B:banana, c:cherry, l:lemon, m:mango]
    println map*.getKey() //[a, B, c]
    

    相关文章

      网友评论

          本文标题:Groovy:集合

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