Skip to content

Commit cef5964

Browse files
BAEL-2766 Maps in Groovy
1 parent 32ae967 commit cef5964

1 file changed

Lines changed: 146 additions & 0 deletions

File tree

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package com.baeldung.groovy.map;
2+
3+
import static groovy.test.GroovyAssert.*
4+
import org.junit.Test
5+
6+
class MapTest{
7+
8+
@Test
9+
void createMap() {
10+
11+
def emptyMap = [:]
12+
assertNotNull(emptyMap)
13+
14+
assertTrue(emptyMap instanceof java.util.LinkedHashMap)
15+
16+
def map = [name:"Jerry", age: 42, city: "New York"]
17+
assertTrue(map.size() == 3)
18+
}
19+
20+
@Test
21+
void addItemsToMap() {
22+
23+
def map = [name:"Jerry"]
24+
25+
map["age"] = 42
26+
27+
map.city = "New York"
28+
29+
def hobbyLiteral = "hobby"
30+
def hobbyMap = [(hobbyLiteral): "Singing"]
31+
map.putAll(hobbyMap)
32+
33+
assertTrue(map == [name:"Jerry", age: 42, city: "New York", hobby:"Singing"])
34+
35+
map.plus([1:20]) // returns new map
36+
37+
map << [2:30]
38+
39+
}
40+
41+
@Test
42+
void getItemsFromMap() {
43+
44+
def map = [name:"Jerry", age: 42, city: "New York", hobby:"Singing"]
45+
46+
assertTrue(map["name"] == "Jerry")
47+
48+
assertTrue(map.name == "Jerry")
49+
50+
def propertyAge = "age"
51+
assertTrue(map[propertyAge] == 42)
52+
}
53+
54+
@Test
55+
void removeItemsFromMap() {
56+
57+
def map = [1:20, a:30, 2:42, 4:34, ba:67, 6:39, 7:49]
58+
59+
def minusMap = map.minus([2:42, 4:34]);
60+
assertTrue(minusMap == [1:20, a:30, ba:67, 6:39, 7:49])
61+
62+
minusMap.removeAll{it -> it.key instanceof String}
63+
assertTrue( minusMap == [ 1:20, 6:39, 7:49])
64+
65+
minusMap.retainAll{it -> it.value %2 == 0}
66+
assertTrue( minusMap == [1:20])
67+
}
68+
69+
@Test
70+
void iteratingOnMaps(){
71+
def map = [name:"Jerry", age: 42, city: "New York", hobby:"Singing"]
72+
73+
map.each{ entry -> println "$entry.key: $entry.value" }
74+
75+
map.eachWithIndex{ entry, i -> println "$i $entry.key: $entry.value" }
76+
77+
map.eachWithIndex{ key, value, i -> println "$i $key: $value" }
78+
}
79+
80+
@Test
81+
void filteringAndSearchingMaps(){
82+
def map = [name:"Jerry", age: 42, city: "New York", hobby:"Singing"]
83+
84+
assertTrue(map.find{ it.value == "New York"}.key == "city")
85+
86+
assertTrue(map.findAll{ it.value == "New York"} == [city : "New York"])
87+
88+
map.grep{it.value == "New York"}.each{ it -> assertTrue(it.key == "city" && it.value == "New York")}
89+
90+
assertTrue(map.every{it -> it.value instanceof String} == false)
91+
92+
assertTrue(map.any{it -> it.value instanceof String} == true)
93+
}
94+
95+
@Test
96+
void collect(){
97+
98+
def map = [1: [name:"Jerry", age: 42, city: "New York"],
99+
2: [name:"Long", age: 25, city: "New York"],
100+
3: [name:"Dustin", age: 29, city: "New York"],
101+
4: [name:"Dustin", age: 34, city: "New York"]]
102+
103+
def names = map.collect{entry -> entry.value.name} // returns only list
104+
assertTrue(names == ["Jerry", "Long", "Dustin", "Dustin"])
105+
106+
def uniqueNames = map.collect([] as HashSet){entry -> entry.value.name}
107+
assertTrue(uniqueNames == ["Jerry", "Long", "Dustin"] as Set)
108+
109+
def idNames = map.collectEntries{key, value -> [key, value.name]}
110+
assertTrue(idNames == [1:"Jerry", 2: "Long", 3:"Dustin", 4: "Dustin"])
111+
112+
def below30Names = map.findAll{it.value.age < 30}.collect{key, value -> value.name}
113+
assertTrue(below30Names == ["Long", "Dustin"])
114+
115+
116+
}
117+
118+
@Test
119+
void group(){
120+
def map = [1:20, 2: 40, 3: 11, 4: 93]
121+
122+
def subMap = map.groupBy{it.value % 2}
123+
println subMap
124+
assertTrue(subMap == [0:[1:20, 2:40 ], 1:[3:11, 4:93]])
125+
126+
def keySubMap = map.subMap([1, 2])
127+
assertTrue(keySubMap == [1:20, 2:40])
128+
129+
}
130+
131+
@Test
132+
void sorting(){
133+
def map = [ab:20, a: 40, cb: 11, ba: 93]
134+
135+
def naturallyOrderedMap = map.sort()
136+
assertTrue([a:40, ab:20, ba:93, cb:11] == naturallyOrderedMap)
137+
138+
def compSortedMap = map.sort({ k1, k2 -> k1 <=> k2 } as Comparator)
139+
assertTrue([a:40, ab:20, ba:93, cb:11] == compSortedMap)
140+
141+
def cloSortedMap = map.sort({ it1, it2 -> it1.value <=> it1.value })
142+
assertTrue([cb:11, ab:20, a:40, ba:93] == cloSortedMap)
143+
144+
}
145+
146+
}

0 commit comments

Comments
 (0)