Sidebar LogoHomeIndexIndexGitHub </> Back Next Collections

Collections


Lists

Also see [groovy_site]:

project1/lists.groovy
def myList = [1, 23.7, "apple", ["guitar", -1, false], true]

println myList[1] // 23.7
println myList[1..2] // [23.7, apple]
println myList[3][0] // guitar
println myList.get(3).get(0) // guitar

println myList[3].contains(-1) // true
println myList[3].size() // 3

myList[3].add(10)
myList[3] << 20 // the same as `add`
println myList[3] // [guitar, -1, false, 10, 20]

myList[3].remove(1)
println myList[3] // [guitar, false, 10, 20]

def newList = myList[3] + [30, 40]
println newList // [guitar, false, 10, 20, 30, 40]
newList = newList.plus([40])
println newList // [guitar, false, 10, 20, 30, 40, 40]

newList = newList - [40]
println newList // [guitar, false, 10, 20, 30]
newList = newList.minus([20, 30])
println newList // [guitar, false, 10]

println newList.pop() // guitar
println newList // [false, 10]

println newList.removeLast() // 10
println newList // [false]

println([1, 2, 3].intersect([2, 3, 4])) // [2, 3]

def anotherList = [1, 2, 3]
println anotherList.reverse() // [3, 2, 1]
println anotherList           // [1, 2, 3] // `reverse()` returned a copy

println([5, "abc", 10.0].sort()) // [5, 10.0, abc] // It can sort values of different types!

// Oops!
// println "a".compareTo(5)
// java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

println anotherList.isEmpty() // false

As for sorting, Char GPT [chat_gpt] explains it the following way:

Groovy's sort() method does not simply convert all elements to strings. The sorting mechanism in Groovy is a bit more sophisticated. Let's dive into how Groovy handles sorting when dealing with mixed types.

In Groovy, the sort() method uses the Comparable interface to compare elements. When elements of different types are present in the list, Groovy tries to compare them in a way that makes sense, typically using their natural order. However, when a direct comparison isn't possible, Groovy may convert them to strings for comparison.


Maps

Also see [groovy_site, 9. Maps].

project1/maps.groovy
import java.time.LocalDate

def participant = [
        "name": "Bob",
        "sections": ["front-end", "back-end"],
        "registration date": LocalDate.of(2024, 06, 10)]

println participant.name // Bob
println participant["sections"] // front-end
println participant["sections"][0] // front-end
println participant["age"] // null
println participant.get("registration date")   // 2024-06-10
println participant.getAt("registration date") // 2024-06-10 // [1]

def someMap = [:]
println someMap.anyKey // null

println someMap.getClass()     // class java.util.LinkedHashMap
println participant.getClass() // class java.util.LinkedHashMap

someMap.put(1, "one")
someMap.put(2, "two")
someMap.put(3, "three")
someMap.put(4, "four")

// `LinkedHashMap` preserves iteration order
someMap.each {key, value -> print value + " "}
println() // one two three four

someMap.eachWithIndex {key, value, i -> println "$i | $key: $value"}
// 0 | 1: one
// 1 | 2: two
// 2 | 3: three
// 3 | 4: four

[1] getAt() is the method for the [] operator. Also see operator overloading.


Ranges

Also see [groovy_site]:

project1/ranges.groovy
def myRange = 1..4

println myRange instanceof java.util.List // true

println myRange             // 1..4
println myRange.size()      // 4
println myRange.getFrom()   // 1
println myRange.getTo()     // 4
println myRange.from        // 1
println myRange.to          // 4
println myRange.isReverse() // false

println myRange.get(2)          // 3
println myRange[2]              // 3
println myRange.contains(2)     // true
println myRange.contains(2.5)   // false

def mySubRange = myRange.subList(1, 4) // zero-based, 1 inclusive, 4 exclusive
println mySubRange // 2..4

mySubRange.each {print "$it "}
println() // 2 3 4

(5..2).each {print "$it "} // reverse
println() // 5 4 3 2

(5<..<2).each {print "$it "} // exclusive
println() // 4 3

 


Back Next