Groovy基本使用

一、简介

Groovy是构建在JVM上的一个轻量级却强大的动态语言, 它结合了Python、Ruby和Smalltalk的许多强大的特性。

Groovy就是用Java写的 , Groovy语法与Java语法类似, Groovy 代码能够与 Java 代码很好地结合,也能用于扩展现有代码, 相对于Java, 它在编写代码的灵活性上有非常明显的提升,Groovy 可以使用其他 Java 语言编写的库。

Groovy基于Java编写的一门语言。

二、测试

导入依赖

<dependency>
  <groupId>org.codehaus.groovy</groupId>
  <artifactId>groovy-all</artifactId>
  <version>3.0.7</version>
  <type>pom</type>
</dependency>

集成测试类

package com.qiang.groovy.other

/**
 @author 小强崽
 @create: 2021-04-08 23:39:38
 @description: Groovy基本语法测试
 */
class GroovyTest extends GroovyTestCase {

    /**
     * 测试输出
     */
    void testPrintln() {
        // 111
        println(111)
        // 111
        println 111
        // println
        println("println")
    }

    /**
     * 定义变量
     */
    void testDef() {
        def num = 1
        def str1 = 'hello'
        def str2 = "word"
        def str3 = ''' asdasd '''
        def str4 = "${str1} word"
        // 1
        println(num)
        // hello
        println(str1)
        // word
        println(str2)
        //  asdasd
        println(str3)
        // hello word
        println(str4)

    }


    /**
     * 测试断言
     */
    void testAssert() {
        def num = 1
        // ======断言等于2会报错====== //
        // assert num == 2
        //        |   |
        //        1   false
        // ======================== //
        assert num == 1
    }

    /**
     * 定义数组
     */
    void testArray() {
        def array = ['xiao', 'qiang']
        // [xiao, qiang]
        println(array)
    }

    /**
     * 向数组添加一个元素
     */
    void testAddArray() {
        def array = ['xiao', 'qiang']
        array << 'zai'
        // [xiao, qiang, zai]
        println(array)
    }

    /**
     * 遍历数组
     */
    void testForArray() {
        def list = ['xiao', 'qiang', 'zai']

        // 第一种for循环遍历
        for (def element : list) {
            println(element)
        }

        // 第二种for循环遍历
        for (i in 0..<list.size()) {
            println(list[i])
        }

        // lambda表达式
        list.each { element -> println(element) }

        // 闭包遍历
        list.forEach({ element -> println(element) })

        // 迭代器遍历
        list.iterator().forEachRemaining({ element -> println(element) })

    }

    /**
     * 定义一个集合
     */
    void testMap() {
        def map = ['name': 'qiang', 'age': 18]
        // [name:qiang, age:18]
        println(map)
    }

    /**
     * 向集合添加数据
     */
    void testAddMap() {
        def map = ['name': 'qiang', 'age': 18]
        map.sex = '男'
        map['address'] = '马尔达夫'
        // [name:qiang, age:18, sex:男, address:马尔达夫]
        println(map)
    }

    /**
     * 输出Java类型
     */
    void testGetClass() {
        def map = ['name': 'qiang', 'age': 18]
        // class java.util.LinkedHashMap
        println map.getClass()
    }

    /**
     * 定义闭包
     */
    void testClosure() {
        // 闭包定义
        def c1 = { p ->
            println(p)
        }
        def c2 = {
            println 333
        }
        // hello world
        m1(c1)
        // 333
        m2(c2)

    }

    /**
     * 传参,调用从c1闭包
     * @param closure
     * @return Void
     */
    static def m1(Closure closure) {
        closure('hello world')
    }

    /**
     * 不传参,调用从c2闭包
     * @param closure
     * @return Void
     */
    static def m2(Closure closure) {
        closure()
    }

    /**
     * 找出符合集合的元素
     */
    void testListFindAll1() {
        def list = ["1", "2", "3", "1", "4", "5", "6"]
        def result = list.findAll { it ->
            it == "1"
        }
        // [1, 2, 3, 1, 4, 5, 6]
        println(list)
        // [1, 1]
        println(result)
    }

    /**
     * 找出符合集合的元素
     */
    void testListFindAll2() {
        def list = ["1", "2", "3", "1", "4", "5", "6"]
        def result = list.findAll { it ->
            // is相当于 ==
            it.is("5")
        }
        // [1, 2, 3, 1, 4, 5, 6]
        println(list)
        // [5]
        println(result)
    }

    /**
     * 是否包含元素,返回一个布尔值
     */
    void testListAny() {
        def list = ["1", "2", "3", "1", "4", "5", "6"]
        def result = list.any() { it ->
            // is相当于 ==
            it.is("5")
        }
        // [1, 2, 3, 1, 4, 5, 6]
        println(list)
        // true
        println(result)
    }


    /**
     * 找出集合中匹配的元素
     */
    void testListFind1() {
        def list = ["1", "2", "3", "1", "4", "5", "6"]
        def result = list.find() { it ->
            // is相当于 ==
            it.is("5")
        }
        // [1, 2, 3, 1, 4, 5, 6]
        println(list)
        // 5
        println(result)
    }

    /**
     * 找出集合中匹配的元素
     */
    void testListFind2() {
        def list = ["qiang", "2", "3", "1", "4", "5", "6"]
        def result = list.find() { it ->
            // 返回第一个不为空的元素
            true
        }
        // [1, 2, 3, 1, 4, 5, 6]
        println(list)
        // qiang
        println(result)
    }

    /**
     * 遍历List
     */
    void testListEach() {
        def list = ["qiang", "2", "3", "1", "4", "5", "6"]
        list.each { it ->
            // qiang 2 3 1 4 5 6
            println(it)
        }
    }

    /**
     * *.展开操作符
     */
    void testAsterisk() {
        def list1 = ["xiao", "qiang", "zai", "www", "wu", "duo", "qiang", "com"]
        def list2 = list1*.toUpperCase()
        // [XIAO, QIANG, ZAI, WWW, WU, DUO, QIANG, COM]
        println(list2)
    }

    /**
     * 遍历Map
     */
    void testMapEach() {
        def map = ['name': 'qiang', 'age': 18, 'address': 'hainan']
        map.each { it ->
            // name age address
            println(it.key)
            // qiang 18 hainan
            println(it.value)
        }

        // 遍历 map 集合
        def result = map.find { key, value ->
            // 模糊查询
            value =~ "qiang"
        }
        // name=qiang
        println(result)
    }

    /**
     * 找出符合集合的元素
     */
    void testMapFindAll() {
        def map = ['name': 'qiang', 'age': 18, 'address': 'hainan']
        def result = map.findAll { key, value ->
            key.is("name")
            value == "qiang"
        }
        // [name:qiang]
        println(result)
    }

    /**
     * 是否包含元素,返回一个布尔值
     */
    void testMapAny() {
        def map = ['name': 'qiang', 'age': 18, 'address': 'hainan']
        def result = map.any { key, value ->
            key.is("name")
            value == "zai"
        }
        // false
        println(result)
    }


    /**
     * 找出集合中匹配的元素
     */
    void testMapFind() {
        def map = ['name': 'qiang', 'age': 18, 'address': 'hainan']
        def result = map.find { key, value ->
            key.is("name")
            value == "qiang"
        }
        // name=qiang
        println(result)
    }


}

作者(Author):小强崽
来源(Source):https://www.wuduoqiang.com/archives/Groovy基本使用
协议(License):署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
版权(Copyright):商业转载请联系作者获得授权,非商业转载请注明出处。 For commercial use, please contact the author for authorization. For non-commercial use, please indicate the source.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值