Skip to content

Commit b1aaff5

Browse files
authored
Merge pull request eugenp#6615 from josephine-barboza/master
BAEL-2766 Maps in Groovy
2 parents fb036ae + db61484 commit b1aaff5

File tree

5 files changed

+435
-0
lines changed

5 files changed

+435
-0
lines changed

core-groovy-collections/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Groovy
2+
3+
## Relevant articles:
4+
5+
- [Maps in Groovy](http://www.baeldung.com/)
6+

core-groovy-collections/pom.xml

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<artifactId>core-groovy-collections</artifactId>
6+
<version>1.0-SNAPSHOT</version>
7+
<name>core-groovy-collections</name>
8+
<packaging>jar</packaging>
9+
10+
<parent>
11+
<groupId>com.baeldung</groupId>
12+
<artifactId>parent-modules</artifactId>
13+
<version>1.0.0-SNAPSHOT</version>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.codehaus.groovy</groupId>
19+
<artifactId>groovy</artifactId>
20+
<version>${groovy.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>org.codehaus.groovy</groupId>
24+
<artifactId>groovy-all</artifactId>
25+
<version>${groovy-all.version}</version>
26+
<type>pom</type>
27+
</dependency>
28+
<dependency>
29+
<groupId>org.codehaus.groovy</groupId>
30+
<artifactId>groovy-dateutil</artifactId>
31+
<version>${groovy.version}</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.codehaus.groovy</groupId>
35+
<artifactId>groovy-sql</artifactId>
36+
<version>${groovy-sql.version}</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.junit.platform</groupId>
40+
<artifactId>junit-platform-runner</artifactId>
41+
<version>${junit.platform.version}</version>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.hsqldb</groupId>
46+
<artifactId>hsqldb</artifactId>
47+
<version>${hsqldb.version}</version>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.spockframework</groupId>
52+
<artifactId>spock-core</artifactId>
53+
<version>${spock-core.version}</version>
54+
<scope>test</scope>
55+
</dependency>
56+
</dependencies>
57+
58+
<build>
59+
<plugins>
60+
<plugin>
61+
<groupId>org.codehaus.gmavenplus</groupId>
62+
<artifactId>gmavenplus-plugin</artifactId>
63+
<version>${gmavenplus-plugin.version}</version>
64+
<executions>
65+
<execution>
66+
<goals>
67+
<goal>addSources</goal>
68+
<goal>addTestSources</goal>
69+
<goal>compile</goal>
70+
<goal>compileTests</goal>
71+
</goals>
72+
</execution>
73+
</executions>
74+
</plugin>
75+
<plugin>
76+
<artifactId>maven-failsafe-plugin</artifactId>
77+
<version>${maven-failsafe-plugin.version}</version>
78+
<dependencies>
79+
<dependency>
80+
<groupId>org.junit.platform</groupId>
81+
<artifactId>junit-platform-surefire-provider</artifactId>
82+
<version>${junit.platform.version}</version>
83+
</dependency>
84+
</dependencies>
85+
<executions>
86+
<execution>
87+
<id>junit5</id>
88+
<goals>
89+
<goal>integration-test</goal>
90+
<goal>verify</goal>
91+
</goals>
92+
<configuration>
93+
<includes>
94+
<include>**/*Test5.java</include>
95+
</includes>
96+
</configuration>
97+
</execution>
98+
</executions>
99+
</plugin>
100+
<plugin>
101+
<artifactId>maven-surefire-plugin</artifactId>
102+
<version>2.20.1</version>
103+
<configuration>
104+
<useFile>false</useFile>
105+
<includes>
106+
<include>**/*Test.java</include>
107+
<include>**/*Spec.java</include>
108+
</includes>
109+
</configuration>
110+
</plugin>
111+
</plugins>
112+
</build>
113+
114+
<repositories>
115+
<repository>
116+
<id>central</id>
117+
<url>http://jcenter.bintray.com</url>
118+
</repository>
119+
</repositories>
120+
121+
<properties>
122+
<junit.platform.version>1.0.0</junit.platform.version>
123+
<groovy.version>2.5.6</groovy.version>
124+
<groovy-all.version>2.5.6</groovy-all.version>
125+
<groovy-sql.version>2.5.6</groovy-sql.version>
126+
<hsqldb.version>2.4.0</hsqldb.version>
127+
<spock-core.version>1.1-groovy-2.4</spock-core.version>
128+
<gmavenplus-plugin.version>1.6</gmavenplus-plugin.version>
129+
</properties>
130+
</project>
131+
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package com.baeldung.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+
assertTrue(hobbyMap.hobby == "Singing")
35+
assertTrue(hobbyMap[hobbyLiteral] == "Singing")
36+
37+
map.plus([1:20]) // returns new map
38+
39+
map << [2:30]
40+
41+
}
42+
43+
@Test
44+
void getItemsFromMap() {
45+
46+
def map = [name:"Jerry", age: 42, city: "New York", hobby:"Singing"]
47+
48+
assertTrue(map["name"] == "Jerry")
49+
50+
assertTrue(map.name == "Jerry")
51+
52+
def propertyAge = "age"
53+
assertTrue(map[propertyAge] == 42)
54+
}
55+
56+
@Test
57+
void removeItemsFromMap() {
58+
59+
def map = [1:20, a:30, 2:42, 4:34, ba:67, 6:39, 7:49]
60+
61+
def minusMap = map.minus([2:42, 4:34]);
62+
assertTrue(minusMap == [1:20, a:30, ba:67, 6:39, 7:49])
63+
64+
minusMap.removeAll{it -> it.key instanceof String}
65+
assertTrue( minusMap == [ 1:20, 6:39, 7:49])
66+
67+
minusMap.retainAll{it -> it.value %2 == 0}
68+
assertTrue( minusMap == [1:20])
69+
}
70+
71+
@Test
72+
void iteratingOnMaps(){
73+
def map = [name:"Jerry", age: 42, city: "New York", hobby:"Singing"]
74+
75+
map.each{ entry -> println "$entry.key: $entry.value" }
76+
77+
map.eachWithIndex{ entry, i -> println "$i $entry.key: $entry.value" }
78+
79+
map.eachWithIndex{ key, value, i -> println "$i $key: $value" }
80+
}
81+
82+
@Test
83+
void filteringAndSearchingMaps(){
84+
def map = [name:"Jerry", age: 42, city: "New York", hobby:"Singing"]
85+
86+
assertTrue(map.find{ it.value == "New York"}.key == "city")
87+
88+
assertTrue(map.findAll{ it.value == "New York"} == [city : "New York"])
89+
90+
map.grep{it.value == "New York"}.each{ it -> assertTrue(it.key == "city" && it.value == "New York")}
91+
92+
assertTrue(map.every{it -> it.value instanceof String} == false)
93+
94+
assertTrue(map.any{it -> it.value instanceof String} == true)
95+
}
96+
97+
@Test
98+
void collect(){
99+
100+
def map = [1: [name:"Jerry", age: 42, city: "New York"],
101+
2: [name:"Long", age: 25, city: "New York"],
102+
3: [name:"Dustin", age: 29, city: "New York"],
103+
4: [name:"Dustin", age: 34, city: "New York"]]
104+
105+
def names = map.collect{entry -> entry.value.name} // returns only list
106+
assertTrue(names == ["Jerry", "Long", "Dustin", "Dustin"])
107+
108+
def uniqueNames = map.collect([] as HashSet){entry -> entry.value.name}
109+
assertTrue(uniqueNames == ["Jerry", "Long", "Dustin"] as Set)
110+
111+
def idNames = map.collectEntries{key, value -> [key, value.name]}
112+
assertTrue(idNames == [1:"Jerry", 2: "Long", 3:"Dustin", 4: "Dustin"])
113+
114+
def below30Names = map.findAll{it.value.age < 30}.collect{key, value -> value.name}
115+
assertTrue(below30Names == ["Long", "Dustin"])
116+
117+
118+
}
119+
120+
@Test
121+
void group(){
122+
def map = [1:20, 2: 40, 3: 11, 4: 93]
123+
124+
def subMap = map.groupBy{it.value % 2}
125+
println subMap
126+
assertTrue(subMap == [0:[1:20, 2:40 ], 1:[3:11, 4:93]])
127+
128+
def keySubMap = map.subMap([1, 2])
129+
assertTrue(keySubMap == [1:20, 2:40])
130+
131+
}
132+
133+
@Test
134+
void sorting(){
135+
def map = [ab:20, a: 40, cb: 11, ba: 93]
136+
137+
def naturallyOrderedMap = map.sort()
138+
assertTrue([a:40, ab:20, ba:93, cb:11] == naturallyOrderedMap)
139+
140+
def compSortedMap = map.sort({ k1, k2 -> k1 <=> k2 } as Comparator)
141+
assertTrue([a:40, ab:20, ba:93, cb:11] == compSortedMap)
142+
143+
def cloSortedMap = map.sort({ it1, it2 -> it1.value <=> it1.value })
144+
assertTrue([cb:11, ab:20, a:40, ba:93] == cloSortedMap)
145+
146+
}
147+
148+
}

0 commit comments

Comments
 (0)