List - Collection type, encapsulated with [] when used, separated by , for intermediate data
Define List: List list = []
example:
> List colors = ["red","blue","green","yellow"]
Methods of the List type:
- list.add(<Object any_type>): add an element to the list
Return value type: Boolean
example:
> List colors = ["red", "blue", "green"]
>
> colors.add("yellow") //return: true
- list.addAll(<List list1>): Add multiple elements to the list
Return value type: Boolean
example:
> List colors = ["red", "blue"]
>
> colors.addAll(["green", "yellow"]) //Return: true
- list.clear(): Clear the string
Return value type: no return value
example:
> List colors = ["red", "blue"]
>
> colors. clear()
- list.contains(<Object any_type>): Determine whether the specified element exists in the list. Returns "true" if the element exists in the list
Return value type: Boolean
example:
> List colors = ["red", "blue", "green", "yellow"]
>
> result = colors.contains("red") //return: true
- list.containsAll(<List list1>): Determine whether all specified elements exist in the list. Returns "true" if the element exists in the list
Return value type: Boolean
example:
> List colors = ["red", "blue", "green"]
>
> result = colors.containsAll(["red", "yellow"]) //return: false
- list.indexOf(<Object any_type>): returns the position of the given element in the list (the position of the first element is "0")
Return value type: BigDecimal
example:
> List colors = ["red", "blue", "green", "yellow"]
>
> result = colors.indexOf("green") //return: 2
- list.get(<BigDecimal indexNumber>): returns the element in the list at the given position (the first element is at position "0")
Return value type: Object
example:
> List colors = ["red", "blue", "green", "yellow"]
>
> result = colors.get(1) // return: "blue"
- list.size(): returnthe number of elements in the list
Return value type: BigDecimal
example:
> List colors = ["red", "blue", "green", "yellow"]
>
> result = colors.size() // returns: 4
- list.sort(<Boolean bool>) List sorting, optional Boolean value specifies ascending (true)/descending (false), defaults to ascending when empty
Return value type: List
example:
> List colors = ["red", "blue", "green", "yellow"]
>
> colors.sort() // returns: ["blue","green","red","yellow"]
- list.subList(<BigDecimal startIndex>,<BigDecimal endIndex>) returns a set of elements from the given list according to the specified start and end index (inclusive, excluding the end index)
Return value type: List
example:
> List colors = ["red", "blue", "green", "yellow"]
>
> result = colors.subList(1, 3); // returns: ["blue","green"]
- list.remove(<BigDecimal index>) removes and returns the element at the specified index (the first element has index "0")
Return value type: Object
example:
> List colors = ["red", "blue", "green", "yellow"]
>
> colors. remove(2) // returns: "green"
- list.isEmpty() determines whether the list is empty. Returns a boolean value - true if the list contains no values, otherwise - false if the list contains values
Return value type: Boolean
example:
> List colors = ["red", "blue", "green", "yellow"]
>
> result = colors.isEmpty() // returns: false
- list.intersect(<List list>) returns the intersection of the specified list and the current list
Return value type: List
example:
> colors = ["red", "blue", "green", "orange"]
>
> fruits = ["apple","orange","banana"]
>
> result = colors.intersect(fruits) // returns: ["orange"]
- list.lastIndexOf(<Object any_type>) returns the position of the last occurrence of the specified element in the list
Return value type: BigDecimal
example:
> colors = ["red", "blue", "green", "yellow", "green"]
>
> result = colors.lastIndexOf("green") //return: 4
- list.eachWithIndex(<Closure closure>) Traverse the data in the list, and pass in the item and index in the closure
Return value type: List
example:
> List list = ["a", "c"]
>
> list.eachWithIndex { item, int i ->
>
> log. info(item)
>
> log. info(i)
>
> } //Run log: a 0 b 1
- list.each(<Closure closure>) Traverse the data in the list, pass in the item in the closure
Return value type: List
example:
> List list = ["a", "c"]
>
> list. each { item ->
>
> log. info(item)
>
> } //Run log: a b
- list.any{<Closure closure>} Determine whether at least one element is valid
Return value type: Boolean
example 1:
> List list = ["a", "b"]
>
> Boolean b = list.any{x ->x=="a"} //Whether there is an element in the list as "a"
>
> log. info(b)
>
> } //running log: true
Example 2:
> List list = [["a":1], ["a":2]]
>
> Boolean b = list.any{x ->(x.a as Integer)>1} //Whether the element in the list is a map and the value whose key is "a" is greater than 1
>
> log. info(b)
>
> } //running log: true
- list.collect{<Closure closure>} The elements in the list are maps, and the value in each map is taken out
Return value type: List
example:
> List list = [["a":1], ["a":2]]
>
> List list = list.collect{x ->x.a}
>
> log. info(list)
>
> } //Run log: [1,2]
- list.find{<Closure closure>} The elements in the list are maps, and the first qualified value in each map is taken out
Return value type: Map
example:
> List list = [["a":1], ["a":2]]
>
> Map map = list.find{x ->(x.a as Integer)>1}
>
> log. info(map)
>
> } //Run log: {a=2}