Groovy, a dynamic language for the Java platform, offers powerful tools for working with collections and data structures, making it easier to manage and manipulate data. Key topics include Lists and Arrays, Maps, Iteration over Collections, and Data Manipulation. This guide will help you dive deep into Groovy’s versatile collection handling features.
def list = []list = List()def list = [1, 2, 3]list = {}list.add(5)list.append(5)list.insert(5)list.push(5)list.contains()list.exists()list.search()list.find()list[1]list[2]list.get(1)list.get(2)list.removeLast()list.delete()list.pop()list.remove()def arr = [1, 2, 3]; arr[1..2]?
[2, 3][1, 2, 3][1, 2]nullforEach()list.each()iterate()for()indexOf()findIndex()getIndex()position()def map = [:]def map = {}map = Map()map = {}[]map.add('key', 'value')map['key'] = 'value'map.insert('key', 'value')map.put('key', 'value')map.get() method do in Groovy?
map.delete('key')map.remove('key')map.pop('key')map.removeAt('key')map.hasKey('key')map.containsKey('key')map.findKey('key')map.keyExists('key')map.keySet() return in Groovy?
map.values()map.getValues()map.all()map.each()map.each { key, value -> println "$key: $value" } do in Groovy?
map.clear()map.removeAll()map.deleteAll()map.clearAll()map.keySet().each { println it }map.eachKey { println it }map.each { it.key }map.keys().each { println it }forEach()each()iterate()do()1..5.each { println it }for 1..5 { println it }iterate(1..5) { println it }for i in 1..5 { println i }eachWithIndex() method do in Groovy?
map.each { key, value -> println "$key: $value" }map.keys().each { println key, value }map.eachKey { println key }map.eachValue { println value }collection.reverseEach()collection.eachReverse()collection.reverse()collection.each { println it.reverse() }list.each { println it } in Groovy?
collection.filter()collection.select()collection.each()collection.findAll()each()collect()map()transform()find() method do on a Groovy collection?
list.each { if (it > 5) println it }list.foreach { if (it > 5) println it }list.eachIf { it > 5 println it }list.eachIf { println it > 5 }| Qno | Answer |
|---|---|
| 1 | a) def list = [] |
| 2 | a) list.add(5) |
| 3 | a) list.contains() |
| 4 | b) 0 |
| 5 | a) list[1] |
| 6 | c) list.pop() |
| 7 | c) Arrays are mutable and can store any type of object |
| 8 | a) [2, 3] |
| 9 | b) list.each() |
| 10 | a) indexOf() |
| 11 | a) def map = [:] |
| 12 | b) map['key'] = 'value' |
| 13 | a) Returns the value associated with the key |
| 14 | b) map.remove('key') |
| 15 | b) map.containsKey('key') |
| 16 | b) The keys of the map |
| 17 | a) map.values() |
| 18 | a) It iterates over the map and prints each key-value pair |
| 19 | a) map.clear() |
| 20 | a) map.keySet().each { println it } |
| 21 | b) each() |
| 22 | a) 1..5.each { println it } |
| 23 | a) Iterates over each element and its index in a collection |
| 24 | a) map.each { key, value -> println "$key: $value" } |
| 25 | a) collection.reverseEach() |
| 26 | a) It will print each element in the list |
| 27 | d) collection.findAll() |
| 28 | b) collect() |
| 29 | a) Returns the first element that matches the condition |
| 30 | a) list.each { if (it > 5) println it } |