Move Existing Articles to core-groovy-collections Module
This commit is contained in:
@@ -5,4 +5,6 @@ This module contains articles about Groovy core collections
|
||||
## Relevant articles:
|
||||
|
||||
- [Maps in Groovy](https://www.baeldung.com/groovy-maps)
|
||||
|
||||
- [Finding Elements in Collections in Groovy](https://www.baeldung.com/groovy-collections-find-elements)
|
||||
- [Lists in Groovy](https://www.baeldung.com/groovy-lists)
|
||||
- [A Quick Guide to Iterating a Map in Groovy](https://www.baeldung.com/groovy-map-iterating)
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.baeldung.lists
|
||||
package com.baeldung.find
|
||||
|
||||
import com.baeldung.Person
|
||||
import com.baeldung.find.Person
|
||||
import org.junit.Test
|
||||
|
||||
import static org.junit.Assert.*
|
||||
|
||||
class ListUnitTest {
|
||||
class ListFindUnitTest {
|
||||
|
||||
private final personList = [
|
||||
new Person("Regina", "Fitzpatrick", 25),
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.baeldung.map
|
||||
package com.baeldung.find
|
||||
|
||||
import com.baeldung.Person
|
||||
import com.baeldung.find.Person
|
||||
import org.junit.Test
|
||||
|
||||
import static org.junit.Assert.*
|
||||
|
||||
class MapUnitTest {
|
||||
class MapFindUnitTest {
|
||||
|
||||
private final personMap = [
|
||||
Regina : new Person("Regina", "Fitzpatrick", 25),
|
||||
@@ -13,84 +13,6 @@ class MapUnitTest {
|
||||
Lucian : new Person("Lucian", "Walter", 30)
|
||||
]
|
||||
|
||||
@Test
|
||||
void whenUsingEach_thenMapIsIterated() {
|
||||
def map = [
|
||||
'FF0000' : 'Red',
|
||||
'00FF00' : 'Lime',
|
||||
'0000FF' : 'Blue',
|
||||
'FFFF00' : 'Yellow'
|
||||
]
|
||||
|
||||
map.each { println "Hex Code: $it.key = Color Name: $it.value" }
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingEachWithEntry_thenMapIsIterated() {
|
||||
def map = [
|
||||
'E6E6FA' : 'Lavender',
|
||||
'D8BFD8' : 'Thistle',
|
||||
'DDA0DD' : 'Plum',
|
||||
]
|
||||
|
||||
map.each { entry -> println "Hex Code: $entry.key = Color Name: $entry.value" }
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingEachWithKeyAndValue_thenMapIsIterated() {
|
||||
def map = [
|
||||
'000000' : 'Black',
|
||||
'FFFFFF' : 'White',
|
||||
'808080' : 'Gray'
|
||||
]
|
||||
|
||||
map.each { key, val ->
|
||||
println "Hex Code: $key = Color Name $val"
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingEachWithIndexAndEntry_thenMapIsIterated() {
|
||||
def map = [
|
||||
'800080' : 'Purple',
|
||||
'4B0082' : 'Indigo',
|
||||
'6A5ACD' : 'Slate Blue'
|
||||
]
|
||||
|
||||
map.eachWithIndex { entry, index ->
|
||||
def indent = ((index == 0 || index % 2 == 0) ? " " : "")
|
||||
println "$indent Hex Code: $entry.key = Color Name: $entry.value"
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingEachWithIndexAndKeyAndValue_thenMapIsIterated() {
|
||||
def map = [
|
||||
'FFA07A' : 'Light Salmon',
|
||||
'FF7F50' : 'Coral',
|
||||
'FF6347' : 'Tomato',
|
||||
'FF4500' : 'Orange Red'
|
||||
]
|
||||
|
||||
map.eachWithIndex { key, val, index ->
|
||||
def indent = ((index == 0 || index % 2 == 0) ? " " : "")
|
||||
println "$indent Hex Code: $key = Color Name: $val"
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingForLoop_thenMapIsIterated() {
|
||||
def map = [
|
||||
'2E8B57' : 'Seagreen',
|
||||
'228B22' : 'Forest Green',
|
||||
'008000' : 'Green'
|
||||
]
|
||||
|
||||
for (entry in map) {
|
||||
println "Hex Code: $entry.key = Color Name: $entry.value"
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenMapContainsKeyElement_thenCheckReturnsTrue() {
|
||||
def map = [a: 'd', b: 'e', c: 'f']
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung
|
||||
package com.baeldung.find
|
||||
|
||||
class Person {
|
||||
private String firstname
|
||||
@@ -1,10 +1,10 @@
|
||||
package com.baeldung.set
|
||||
package com.baeldung.find
|
||||
|
||||
import org.junit.Test
|
||||
|
||||
import static org.junit.Assert.assertTrue
|
||||
|
||||
class SetUnitTest {
|
||||
class SetFindUnitTest {
|
||||
|
||||
@Test
|
||||
void whenSetContainsElement_thenCheckReturnsTrue() {
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.baeldung.iteratemap
|
||||
|
||||
import com.baeldung.find.Person
|
||||
import org.junit.Test
|
||||
|
||||
import static org.junit.Assert.*
|
||||
|
||||
class IterateMapUnitTest {
|
||||
|
||||
@Test
|
||||
void whenUsingEach_thenMapIsIterated() {
|
||||
def map = [
|
||||
'FF0000' : 'Red',
|
||||
'00FF00' : 'Lime',
|
||||
'0000FF' : 'Blue',
|
||||
'FFFF00' : 'Yellow'
|
||||
]
|
||||
|
||||
map.each { println "Hex Code: $it.key = Color Name: $it.value" }
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingEachWithEntry_thenMapIsIterated() {
|
||||
def map = [
|
||||
'E6E6FA' : 'Lavender',
|
||||
'D8BFD8' : 'Thistle',
|
||||
'DDA0DD' : 'Plum',
|
||||
]
|
||||
|
||||
map.each { entry -> println "Hex Code: $entry.key = Color Name: $entry.value" }
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingEachWithKeyAndValue_thenMapIsIterated() {
|
||||
def map = [
|
||||
'000000' : 'Black',
|
||||
'FFFFFF' : 'White',
|
||||
'808080' : 'Gray'
|
||||
]
|
||||
|
||||
map.each { key, val ->
|
||||
println "Hex Code: $key = Color Name $val"
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingEachWithIndexAndEntry_thenMapIsIterated() {
|
||||
def map = [
|
||||
'800080' : 'Purple',
|
||||
'4B0082' : 'Indigo',
|
||||
'6A5ACD' : 'Slate Blue'
|
||||
]
|
||||
|
||||
map.eachWithIndex { entry, index ->
|
||||
def indent = ((index == 0 || index % 2 == 0) ? " " : "")
|
||||
println "$indent Hex Code: $entry.key = Color Name: $entry.value"
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingEachWithIndexAndKeyAndValue_thenMapIsIterated() {
|
||||
def map = [
|
||||
'FFA07A' : 'Light Salmon',
|
||||
'FF7F50' : 'Coral',
|
||||
'FF6347' : 'Tomato',
|
||||
'FF4500' : 'Orange Red'
|
||||
]
|
||||
|
||||
map.eachWithIndex { key, val, index ->
|
||||
def indent = ((index == 0 || index % 2 == 0) ? " " : "")
|
||||
println "$indent Hex Code: $key = Color Name: $val"
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenUsingForLoop_thenMapIsIterated() {
|
||||
def map = [
|
||||
'2E8B57' : 'Seagreen',
|
||||
'228B22' : 'Forest Green',
|
||||
'008000' : 'Green'
|
||||
]
|
||||
|
||||
for (entry in map) {
|
||||
println "Hex Code: $entry.key = Color Name: $entry.value"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.baeldung.groovy.lists
|
||||
package com.baeldung.lists
|
||||
|
||||
import static groovy.test.GroovyAssert.*
|
||||
import org.junit.Test
|
||||
|
||||
class ListTest{
|
||||
class ListUnitTest {
|
||||
|
||||
@Test
|
||||
void testCreateList() {
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.map;
|
||||
package com.baeldung.maps;
|
||||
|
||||
import static groovy.test.GroovyAssert.*
|
||||
import org.junit.Test
|
||||
@@ -8,11 +8,8 @@ This module contains articles about core Groovy concepts
|
||||
- [Working with JSON in Groovy](https://www.baeldung.com/groovy-json)
|
||||
- [Reading a File in Groovy](https://www.baeldung.com/groovy-file-read)
|
||||
- [Types of Strings in Groovy](https://www.baeldung.com/groovy-strings)
|
||||
- [A Quick Guide to Iterating a Map in Groovy](https://www.baeldung.com/groovy-map-iterating)
|
||||
- [An Introduction to Traits in Groovy](https://www.baeldung.com/groovy-traits)
|
||||
- [Closures in Groovy](https://www.baeldung.com/groovy-closures)
|
||||
- [Finding Elements in Collections in Groovy](https://www.baeldung.com/groovy-collections-find-elements)
|
||||
- [Lists in Groovy](https://www.baeldung.com/groovy-lists)
|
||||
- [Converting a String to a Date in Groovy](https://www.baeldung.com/groovy-string-to-date)
|
||||
- [Guide to I/O in Groovy](https://www.baeldung.com/groovy-io)
|
||||
- [[More -->]](/core-groovy-2)
|
||||
@@ -1,148 +0,0 @@
|
||||
package com.baeldung.groovy.map;
|
||||
|
||||
import static groovy.test.GroovyAssert.*
|
||||
import org.junit.Test
|
||||
|
||||
class MapTest{
|
||||
|
||||
@Test
|
||||
void createMap() {
|
||||
|
||||
def emptyMap = [:]
|
||||
assertNotNull(emptyMap)
|
||||
|
||||
assertTrue(emptyMap instanceof java.util.LinkedHashMap)
|
||||
|
||||
def map = [name:"Jerry", age: 42, city: "New York"]
|
||||
assertTrue(map.size() == 3)
|
||||
}
|
||||
|
||||
@Test
|
||||
void addItemsToMap() {
|
||||
|
||||
def map = [name:"Jerry"]
|
||||
|
||||
map["age"] = 42
|
||||
|
||||
map.city = "New York"
|
||||
|
||||
def hobbyLiteral = "hobby"
|
||||
def hobbyMap = [(hobbyLiteral): "Singing"]
|
||||
map.putAll(hobbyMap)
|
||||
|
||||
assertTrue(map == [name:"Jerry", age: 42, city: "New York", hobby:"Singing"])
|
||||
assertTrue(hobbyMap.hobby == "Singing")
|
||||
assertTrue(hobbyMap[hobbyLiteral] == "Singing")
|
||||
|
||||
map.plus([1:20]) // returns new map
|
||||
|
||||
map << [2:30]
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void getItemsFromMap() {
|
||||
|
||||
def map = [name:"Jerry", age: 42, city: "New York", hobby:"Singing"]
|
||||
|
||||
assertTrue(map["name"] == "Jerry")
|
||||
|
||||
assertTrue(map.name == "Jerry")
|
||||
|
||||
def propertyAge = "age"
|
||||
assertTrue(map[propertyAge] == 42)
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeItemsFromMap() {
|
||||
|
||||
def map = [1:20, a:30, 2:42, 4:34, ba:67, 6:39, 7:49]
|
||||
|
||||
def minusMap = map.minus([2:42, 4:34]);
|
||||
assertTrue(minusMap == [1:20, a:30, ba:67, 6:39, 7:49])
|
||||
|
||||
minusMap.removeAll{it -> it.key instanceof String}
|
||||
assertTrue( minusMap == [ 1:20, 6:39, 7:49])
|
||||
|
||||
minusMap.retainAll{it -> it.value %2 == 0}
|
||||
assertTrue( minusMap == [1:20])
|
||||
}
|
||||
|
||||
@Test
|
||||
void iteratingOnMaps(){
|
||||
def map = [name:"Jerry", age: 42, city: "New York", hobby:"Singing"]
|
||||
|
||||
map.each{ entry -> println "$entry.key: $entry.value" }
|
||||
|
||||
map.eachWithIndex{ entry, i -> println "$i $entry.key: $entry.value" }
|
||||
|
||||
map.eachWithIndex{ key, value, i -> println "$i $key: $value" }
|
||||
}
|
||||
|
||||
@Test
|
||||
void filteringAndSearchingMaps(){
|
||||
def map = [name:"Jerry", age: 42, city: "New York", hobby:"Singing"]
|
||||
|
||||
assertTrue(map.find{ it.value == "New York"}.key == "city")
|
||||
|
||||
assertTrue(map.findAll{ it.value == "New York"} == [city : "New York"])
|
||||
|
||||
map.grep{it.value == "New York"}.each{ it -> assertTrue(it.key == "city" && it.value == "New York")}
|
||||
|
||||
assertTrue(map.every{it -> it.value instanceof String} == false)
|
||||
|
||||
assertTrue(map.any{it -> it.value instanceof String} == true)
|
||||
}
|
||||
|
||||
@Test
|
||||
void collect(){
|
||||
|
||||
def map = [1: [name:"Jerry", age: 42, city: "New York"],
|
||||
2: [name:"Long", age: 25, city: "New York"],
|
||||
3: [name:"Dustin", age: 29, city: "New York"],
|
||||
4: [name:"Dustin", age: 34, city: "New York"]]
|
||||
|
||||
def names = map.collect{entry -> entry.value.name} // returns only list
|
||||
assertTrue(names == ["Jerry", "Long", "Dustin", "Dustin"])
|
||||
|
||||
def uniqueNames = map.collect([] as HashSet){entry -> entry.value.name}
|
||||
assertTrue(uniqueNames == ["Jerry", "Long", "Dustin"] as Set)
|
||||
|
||||
def idNames = map.collectEntries{key, value -> [key, value.name]}
|
||||
assertTrue(idNames == [1:"Jerry", 2: "Long", 3:"Dustin", 4: "Dustin"])
|
||||
|
||||
def below30Names = map.findAll{it.value.age < 30}.collect{key, value -> value.name}
|
||||
assertTrue(below30Names == ["Long", "Dustin"])
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void group(){
|
||||
def map = [1:20, 2: 40, 3: 11, 4: 93]
|
||||
|
||||
def subMap = map.groupBy{it.value % 2}
|
||||
println subMap
|
||||
assertTrue(subMap == [0:[1:20, 2:40 ], 1:[3:11, 4:93]])
|
||||
|
||||
def keySubMap = map.subMap([1, 2])
|
||||
assertTrue(keySubMap == [1:20, 2:40])
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void sorting(){
|
||||
def map = [ab:20, a: 40, cb: 11, ba: 93]
|
||||
|
||||
def naturallyOrderedMap = map.sort()
|
||||
assertTrue([a:40, ab:20, ba:93, cb:11] == naturallyOrderedMap)
|
||||
|
||||
def compSortedMap = map.sort({ k1, k2 -> k1 <=> k2 } as Comparator)
|
||||
assertTrue([a:40, ab:20, ba:93, cb:11] == compSortedMap)
|
||||
|
||||
def cloSortedMap = map.sort({ it1, it2 -> it1.value <=> it1.value })
|
||||
assertTrue([cb:11, ab:20, a:40, ba:93] == cloSortedMap)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user