Groovy Quick Start

xiaoxiao2021-03-06  53

1, collection

(1) List (java.util.list)

List = [1, 2, 'Hello', new java.util.date ()]

ask.size () == 4

Assert List.get (2) == 'Hello'

Note: Everything is an object (digital automatic conversion)

(2) MAP (java.util.map)

Map = ['Name': 'James', 'Location': 'London']

ask.size () == 2

Assert map.get ('name') == 'james'

(3) Traverse collection

List = [1, 2, 3]

For (i in list) {println i}

2, Closed (Closures)

l Closets Similar to Java, the difference is that only a single method of closing can be called, but there can be any parameters.

Closure = {Param | Println ("Hello $ {param}"}

Closure.call ("World!")

Closure = {Greeting, Name | Println (Greeting Name)}

Closure.call ("Hello", "World!")

l Clipbook is enclosed in "{}", "|" is the parameter, and the following is the processing statement, use the CALL call.

l The first example demonstrates the form of using parameters in a string: $ {param}

l The second example demonstrates a multi-parameter form: use "," separated parameters

l If there is only one parameter, you can use the default parameter "IT", as described below:

Closure = {Println "Hello" it}

Closure.call ("World!")

3, EACH

l Traversal collection, pass the closure one by one

[1, 2, 3]. Each {Item | Print "$ {item} -"}

l The output result of the above example is: 1-2-3-

4, Collect

l Traversal collection, pass the closure one by one, and the result is returned to the corresponding item.

Value = [1, 2, 3] .COLLECT {it * 2}

askERT VALUE == [2, 4, 6]

5, find

l According to the closing break, return to the first item found in the collection

Value = [1, 2, 3] .find {it> 1}

askERT VALUE == 2

6, FindAll

l According to the closing break, return to all the items found in the collection

Value = [1, 2, 3] .findall {it> 1}

askERT VALUE == [2, 3]

7, Inject

l Traversal collection, the first time passed the value and the collection item to the closure, and the result is transmitted as the value passed, and the next collection item is passed to the closure, and the value = [1, 2, 3] is pushed. INJECT ('counting:') {str, item | str item}

askERT VALUE == "Counting: 123"

Value = [1, 2, 3] .INJECT (0) {count, item | count item}

askERT VALUE == 6

8, Every

l Returns true if all items in the collection matches the closing break, otherwise returns false

Value = [1, 2, 3] .every {IT <5}

Assert value

Value = [1, 2, 3] .every {Item | item <3}

Assert! Value

9, Any

l If any project matches in the collection, returns true, otherwise returns false

Value = [1, 2, 3] .ny {it> 2}

Assert value

Value = [1, 2, 3] .any {Item | item> 3}

askERT VALUE == FALSE

10, Min / Max

l Back to the minimum / maximum project in the collection (the object must be compared)

Value = [9, 4, 2, 10, 5] .max ()

askERT VALUE == 10

Value = [9, 4, 2, 10, 5] .min ()

askERT VALUE == 2

Value = ['x', 'Y', 'A', 'Z'] .min ()

askERT VALUE == 'A'

11, Join

l The value in the connection collection into a string

Value = [1, 2, 3] .join ('-')

askERT VALUE == '1-2-3'

12, Yield

l Create a "Yield" style of "Yield" style through the Yield statement in Python and Ruby, which is equally valid in Groovy, just use a closure

Class foo {

Static void main (args) {

Foo = new foo ()

For (x in foo.mygenerator) {

PRINT ("$ {x} -")

}

}

MyGenerator (Closure Yield) {

Yield.call ("a")

Yield.call ("b")

Yield.call ("c")

}

}

l The output of the example is: A-B-C-

l ClouRes prototypes can be omitted, Call and parentheses are also optional, so it is more like Python / Ruby

Class foo {

MyGenerator (Yield) {

Yield "a"

Yield "B"

Yield "C"

}

Static void main (args) {

Foo = new foo ()

Foo.mygenerator {Println "Called with $ {it}"}}

}

转载请注明原文地址:https://www.9cbs.com/read-116898.html

New Post(0)