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)
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.find
|
||||
|
||||
import com.baeldung.find.Person
|
||||
import org.junit.Test
|
||||
|
||||
import static org.junit.Assert.*
|
||||
|
||||
class ListFindUnitTest {
|
||||
|
||||
private final personList = [
|
||||
new Person("Regina", "Fitzpatrick", 25),
|
||||
new Person("Abagail", "Ballard", 26),
|
||||
new Person("Lucian", "Walter", 30),
|
||||
]
|
||||
|
||||
@Test
|
||||
void whenListContainsElement_thenCheckReturnsTrue() {
|
||||
def list = ['a', 'b', 'c']
|
||||
|
||||
assertTrue(list.indexOf('a') > -1)
|
||||
assertTrue(list.contains('a'))
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenListContainsElement_thenCheckWithMembershipOperatorReturnsTrue() {
|
||||
def list = ['a', 'b', 'c']
|
||||
|
||||
assertTrue('a' in list)
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenListOfPerson_whenUsingStreamMatching_thenShouldEvaluateList() {
|
||||
assertTrue(personList.stream().anyMatch {it.age > 20})
|
||||
assertFalse(personList.stream().allMatch {it.age < 30})
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenListOfPerson_whenUsingCollectionMatching_thenShouldEvaluateList() {
|
||||
assertTrue(personList.any {it.age > 20})
|
||||
assertFalse(personList.every {it.age < 30})
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenListOfPerson_whenUsingStreamFind_thenShouldReturnMatchingElements() {
|
||||
assertTrue(personList.stream().filter {it.age > 20}.findAny().isPresent())
|
||||
assertFalse(personList.stream().filter {it.age > 30}.findAny().isPresent())
|
||||
assertTrue(personList.stream().filter {it.age > 20}.findAll().size() == 3)
|
||||
assertTrue(personList.stream().filter {it.age > 30}.findAll().isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenListOfPerson_whenUsingCollectionFind_thenShouldReturnMatchingElements() {
|
||||
assertNotNull(personList.find {it.age > 20})
|
||||
assertNull(personList.find {it.age > 30})
|
||||
assertTrue(personList.findAll {it.age > 20}.size() == 3)
|
||||
assertTrue(personList.findAll {it.age > 30}.isEmpty())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.baeldung.find
|
||||
|
||||
import com.baeldung.find.Person
|
||||
import org.junit.Test
|
||||
|
||||
import static org.junit.Assert.*
|
||||
|
||||
class MapFindUnitTest {
|
||||
|
||||
private final personMap = [
|
||||
Regina : new Person("Regina", "Fitzpatrick", 25),
|
||||
Abagail: new Person("Abagail", "Ballard", 26),
|
||||
Lucian : new Person("Lucian", "Walter", 30)
|
||||
]
|
||||
|
||||
@Test
|
||||
void whenMapContainsKeyElement_thenCheckReturnsTrue() {
|
||||
def map = [a: 'd', b: 'e', c: 'f']
|
||||
|
||||
assertTrue(map.containsKey('a'))
|
||||
assertFalse(map.containsKey('e'))
|
||||
assertTrue(map.containsValue('e'))
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenMapContainsKeyElement_thenCheckByMembershipReturnsTrue() {
|
||||
def map = [a: 'd', b: 'e', c: 'f']
|
||||
|
||||
assertTrue('a' in map)
|
||||
assertFalse('f' in map)
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenMapContainsFalseBooleanValues_thenCheckReturnsFalse() {
|
||||
def map = [a: true, b: false, c: null]
|
||||
|
||||
assertTrue(map.containsKey('b'))
|
||||
assertTrue('a' in map)
|
||||
assertFalse('b' in map)
|
||||
assertFalse('c' in map)
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenMapOfPerson_whenUsingStreamMatching_thenShouldEvaluateMap() {
|
||||
assertTrue(personMap.keySet().stream().anyMatch {it == "Regina"})
|
||||
assertFalse(personMap.keySet().stream().allMatch {it == "Albert"})
|
||||
assertFalse(personMap.values().stream().allMatch {it.age < 30})
|
||||
assertTrue(personMap.entrySet().stream().anyMatch {it.key == "Abagail" && it.value.lastname == "Ballard"})
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenMapOfPerson_whenUsingCollectionMatching_thenShouldEvaluateMap() {
|
||||
assertTrue(personMap.keySet().any {it == "Regina"})
|
||||
assertFalse(personMap.keySet().every {it == "Albert"})
|
||||
assertFalse(personMap.values().every {it.age < 30})
|
||||
assertTrue(personMap.any {firstname, person -> firstname == "Abagail" && person.lastname == "Ballard"})
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenMapOfPerson_whenUsingCollectionFind_thenShouldReturnElements() {
|
||||
assertNotNull(personMap.find {it.key == "Abagail" && it.value.lastname == "Ballard"})
|
||||
assertTrue(personMap.findAll {it.value.age > 20}.size() == 3)
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenMapOfPerson_whenUsingStreamFind_thenShouldReturnElements() {
|
||||
assertTrue(
|
||||
personMap.entrySet().stream()
|
||||
.filter {it.key == "Abagail" && it.value.lastname == "Ballard"}
|
||||
.findAny().isPresent())
|
||||
assertTrue(
|
||||
personMap.entrySet().stream()
|
||||
.filter {it.value.age > 20}
|
||||
.findAll().size() == 3)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.find
|
||||
|
||||
class Person {
|
||||
private String firstname
|
||||
private String lastname
|
||||
private Integer age
|
||||
|
||||
Person(String firstname, String lastname, Integer age) {
|
||||
this.firstname = firstname
|
||||
this.lastname = lastname
|
||||
this.age = age
|
||||
}
|
||||
|
||||
String getFirstname() {
|
||||
return firstname
|
||||
}
|
||||
|
||||
void setFirstname(String firstname) {
|
||||
this.firstname = firstname
|
||||
}
|
||||
|
||||
String getLastname() {
|
||||
return lastname
|
||||
}
|
||||
|
||||
void setLastname(String lastname) {
|
||||
this.lastname = lastname
|
||||
}
|
||||
|
||||
Integer getAge() {
|
||||
return age
|
||||
}
|
||||
|
||||
void setAge(Integer age) {
|
||||
this.age = age
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.baeldung.find
|
||||
|
||||
import org.junit.Test
|
||||
|
||||
import static org.junit.Assert.assertTrue
|
||||
|
||||
class SetFindUnitTest {
|
||||
|
||||
@Test
|
||||
void whenSetContainsElement_thenCheckReturnsTrue() {
|
||||
def set = ['a', 'b', 'c'] as Set
|
||||
|
||||
assertTrue(set.contains('a'))
|
||||
assertTrue('a' in set)
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
package com.baeldung.lists
|
||||
|
||||
import static groovy.test.GroovyAssert.*
|
||||
import org.junit.Test
|
||||
|
||||
class ListUnitTest {
|
||||
|
||||
@Test
|
||||
void testCreateList() {
|
||||
|
||||
def list = [1, 2, 3]
|
||||
assertNotNull(list)
|
||||
|
||||
def listMix = ['A', "b", 1, true]
|
||||
assertTrue(listMix == ['A', "b", 1, true])
|
||||
|
||||
def linkedList = [1, 2, 3] as LinkedList
|
||||
assertTrue(linkedList instanceof LinkedList)
|
||||
|
||||
ArrayList arrList = [1, 2, 3]
|
||||
assertTrue(arrList.class == ArrayList)
|
||||
|
||||
def copyList = new ArrayList(arrList)
|
||||
assertTrue(copyList == arrList)
|
||||
|
||||
def cloneList = arrList.clone()
|
||||
assertTrue(cloneList == arrList)
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCreateEmptyList() {
|
||||
|
||||
def emptyList = []
|
||||
assertTrue(emptyList.size() == 0)
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCompareTwoLists() {
|
||||
|
||||
def list1 = [5, 6.0, 'p']
|
||||
def list2 = [5, 6.0, 'p']
|
||||
assertTrue(list1 == list2)
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetItemsFromList(){
|
||||
|
||||
def list = ["Hello", "World"]
|
||||
|
||||
assertTrue(list.get(1) == "World")
|
||||
assertTrue(list[1] == "World")
|
||||
assertTrue(list[-1] == "World")
|
||||
assertTrue(list.getAt(1) == "World")
|
||||
assertTrue(list.getAt(-2) == "Hello")
|
||||
}
|
||||
|
||||
@Test
|
||||
void testAddItemsToList() {
|
||||
|
||||
def list = []
|
||||
|
||||
list << 1
|
||||
list.add("Apple")
|
||||
assertTrue(list == [1, "Apple"])
|
||||
|
||||
list[2] = "Box"
|
||||
list[4] = true
|
||||
assertTrue(list == [1, "Apple", "Box", null, true])
|
||||
|
||||
list.add(1, 6.0)
|
||||
assertTrue(list == [1, 6.0, "Apple", "Box", null, true])
|
||||
|
||||
def list2 = [1, 2]
|
||||
list += list2
|
||||
list += 12
|
||||
assertTrue(list == [1, 6.0, "Apple", "Box", null, true, 1, 2, 12])
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateItemsInList() {
|
||||
|
||||
def list =[1, "Apple", 80, "App"]
|
||||
list[1] = "Box"
|
||||
list.set(2,90)
|
||||
assertTrue(list == [1, "Box", 90, "App"])
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRemoveItemsFromList(){
|
||||
|
||||
def list = [1, 2, 3, 4, 5, 5, 6, 6, 7]
|
||||
|
||||
list.remove(3)
|
||||
assertTrue(list == [1, 2, 3, 5, 5, 6, 6, 7])
|
||||
|
||||
list.removeElement(5)
|
||||
assertTrue(list == [1, 2, 3, 5, 6, 6, 7])
|
||||
|
||||
assertTrue(list - 6 == [1, 2, 3, 5, 7])
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIteratingOnAList(){
|
||||
|
||||
def list = [1, "App", 3, 4]
|
||||
list.each{ println it * 2}
|
||||
|
||||
list.eachWithIndex{ it, i -> println "$i : $it" }
|
||||
}
|
||||
|
||||
@Test
|
||||
void testCollectingToAnotherList(){
|
||||
|
||||
def list = ["Kay", "Henry", "Justin", "Tom"]
|
||||
assertTrue(list.collect{"Hi " + it} == ["Hi Kay", "Hi Henry", "Hi Justin", "Hi Tom"])
|
||||
}
|
||||
|
||||
@Test
|
||||
void testJoinItemsInAList(){
|
||||
assertTrue(["One", "Two", "Three"].join(",") == "One,Two,Three")
|
||||
}
|
||||
|
||||
@Test
|
||||
void testFilteringOnLists(){
|
||||
def filterList = [2, 1, 3, 4, 5, 6, 76]
|
||||
|
||||
assertTrue(filterList.find{it > 3} == 4)
|
||||
|
||||
assertTrue(filterList.findAll{it > 3} == [4, 5, 6, 76])
|
||||
|
||||
assertTrue(filterList.findAll{ it instanceof Number} == [2, 1, 3, 4, 5, 6, 76])
|
||||
|
||||
assertTrue(filterList.grep( Number )== [2, 1, 3, 4, 5, 6, 76])
|
||||
|
||||
assertTrue(filterList.grep{ it> 6 }== [76])
|
||||
|
||||
def conditionList = [2, 1, 3, 4, 5, 6, 76]
|
||||
|
||||
assertFalse(conditionList.every{ it < 6})
|
||||
|
||||
assertTrue(conditionList.any{ it%2 == 0})
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetUniqueItemsInAList(){
|
||||
assertTrue([1, 3, 3, 4].toUnique() == [1, 3, 4])
|
||||
|
||||
def uniqueList = [1, 3, 3, 4]
|
||||
uniqueList.unique()
|
||||
assertTrue(uniqueList == [1, 3, 4])
|
||||
|
||||
assertTrue(["A", "B", "Ba", "Bat", "Cat"].toUnique{ it.size()} == ["A", "Ba", "Bat"])
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSorting(){
|
||||
|
||||
assertTrue([1, 2, 1, 0].sort() == [0, 1, 1, 2])
|
||||
Comparator mc = {a,b -> a == b? 0: a < b? 1 : -1}
|
||||
|
||||
def list = [1, 2, 1, 0]
|
||||
list.sort(mc)
|
||||
assertTrue(list == [2, 1, 1, 0])
|
||||
|
||||
def strList = ["na", "ppp", "as"]
|
||||
assertTrue(strList.max() == "ppp")
|
||||
|
||||
Comparator minc = {a,b -> a == b? 0: a < b? -1 : 1}
|
||||
def numberList = [3, 2, 0, 7]
|
||||
assertTrue(numberList.min(minc) == 0)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.map;
|
||||
package com.baeldung.maps;
|
||||
|
||||
import static groovy.test.GroovyAssert.*
|
||||
import org.junit.Test
|
||||
Reference in New Issue
Block a user