English
List
About 659 wordsAbout 2 min
2026-01-09
List - A collection type enclosed with [] and elements separated by commas.
Define a List: List list = []
Example:
List colors = ["red","blue","green","yellow"]List type methods:
- list.add(
<Object any_type>): Adds an element to the list Return type: Boolean
Example:
List colors = ["red", "blue", "green"]
colors.add("yellow") //Returns: true- list.addAll(
<List list1>): Adds multiple elements to the list Return type: Boolean
Example:
List colors = ["red", "blue"]
colors.addAll(["green", "yellow"]) //Returns: true- list.clear(): Clears the list Return type: void
Example:
List colors = ["red", "blue"]
colors.clear()- list.contains(
<Object any_type>): Checks if the list contains the specified element. Returns "true" if the element exists Return type: Boolean
Example:
List colors = ["red", "blue", "green", "yellow"]
result = colors.contains("red") //Returns: true- list.containsAll(
<List list1>): Checks if the list contains all specified elements. Returns "true" if all elements exist Return type: Boolean
Example:
List colors = ["red", "blue", "green"]
result = colors.containsAll(["red", "yellow"]) //Returns: false- list.indexOf(
<Object any_type>): Returns the index of the given element in the list (first element index is "0") Return type: int
Example:
List colors = ["red", "blue", "green", "yellow"]
result = colors.indexOf("green") //Returns: 2- list.get(
<int indexNumber>): Returns the element at the specified position (first element index is "0") Return type: Object
Example:
List colors = ["red", "blue", "green", "yellow"]
result = colors.get(1) // Returns: "blue"- list.size(): Returns the number of elements in the list Return type: int
Example:
List colors = ["red", "blue", "green", "yellow"]
result = colors.size() //Returns: 4- list.sort(
<Boolean bool>): Sorts the list. Optional boolean parameter specifies ascending (true)/descending (false) order, defaults to ascending when empty Return type: List
Example:
List colors = ["red", "blue", "green", "yellow"]
colors.sort() // Returns: ["blue","green","red","yellow"]- list.subList(
<int startIndex>,<int endIndex>): Returns a sublist from the given list between the specified start (inclusive) and end (exclusive) indices Return type: List
Example:
List colors = ["red", "blue", "green", "yellow"]
result = colors.subList(1, 3); // Returns: ["blue","green"]- list.remove(
<int index>): Removes and returns the element at the specified index (first element index is "0") Return type: Object
Example:
List colors = ["red", "blue", "green", "yellow"]
colors.remove(2) // Returns: "green"- list.isEmpty(): Checks if the list is empty. Returns true if the list contains no elements, false otherwise Return type: Boolean
Example:
List colors = ["red", "blue", "green", "yellow"]
result = colors.isEmpty() // Returns: false- list.intersect(
<List list>): Returns the intersection between the specified list and current list Return 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 index of the last occurrence of the specified element in the list Return type: BigDecimal
Example:
colors = ["red", "blue", "green", "yellow", "green"]
result = colors.lastIndexOf("green") //Returns: 4- list.eachWithIndex(
<Closure closure>): Iterates through list elements with both item and index passed to the closure Return type: List
Example:
List list = ["a", "c"]
list.eachWithIndex { item, int i ->
log.info(item)
log.info(i)
} //Runtime log: a 0 b 1- list.each(
<Closure closure>): Iterates through list elements with item passed to the closure Return type: List
Example:
List list = ["a", "c"]
list.each { item ->
log.info(item)
} //Runtime log: a b- list.any{
<Closure closure>}: Checks if at least one element satisfies the condition Return type: Boolean
Example 1:
List list = ["a", "b"]
Boolean b = list.any { x -> x == "a" } // Checks if any element equals "a"
log.info(b)
// Runtime log: trueExample 2:
List list = [["a":1], ["a":2]]
Boolean b = list.any { x -> (x.a as Integer) > 1 } // Checks if any map element has value > 1 for key "a"
log.info(b)
// Runtime log: true- list.collect{
<Closure closure>}: Collects values from each map element in the list Return type: List
Example:
List list = [["a":1], ["a":2]]
List result = list.collect { x -> x.a }
log.info(result)
// Runtime log: [1, 2]- list.find{
<Closure closure>}: Finds the first map element in the list that satisfies the condition Return type: Map
Example:
List list = [["a":1], ["a":2]]
Map map = list.find { x -> (x.a as Integer) > 1 }
log.info(map)
// Runtime log: {a=2}