forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BAEL-13598] - Fixed conficts in README.md
- Loading branch information
Showing
135 changed files
with
2,764 additions
and
185 deletions.
There are no files selected for viewing
3 changes: 2 additions & 1 deletion
3
autovalue/src/main/java/com/baeldung/autofactory/provided/IntermediateAssembler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Groovy | ||
|
||
## Relevant articles: | ||
|
||
- [Maps in Groovy](http://www.baeldung.com/) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<artifactId>core-groovy-collections</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>core-groovy-collections</name> | ||
<packaging>jar</packaging> | ||
|
||
<parent> | ||
<groupId>com.baeldung</groupId> | ||
<artifactId>parent-modules</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.codehaus.groovy</groupId> | ||
<artifactId>groovy</artifactId> | ||
<version>${groovy.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.codehaus.groovy</groupId> | ||
<artifactId>groovy-all</artifactId> | ||
<version>${groovy-all.version}</version> | ||
<type>pom</type> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.codehaus.groovy</groupId> | ||
<artifactId>groovy-dateutil</artifactId> | ||
<version>${groovy.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.codehaus.groovy</groupId> | ||
<artifactId>groovy-sql</artifactId> | ||
<version>${groovy-sql.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.platform</groupId> | ||
<artifactId>junit-platform-runner</artifactId> | ||
<version>${junit.platform.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.hsqldb</groupId> | ||
<artifactId>hsqldb</artifactId> | ||
<version>${hsqldb.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.spockframework</groupId> | ||
<artifactId>spock-core</artifactId> | ||
<version>${spock-core.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.codehaus.gmavenplus</groupId> | ||
<artifactId>gmavenplus-plugin</artifactId> | ||
<version>${gmavenplus-plugin.version}</version> | ||
<executions> | ||
<execution> | ||
<goals> | ||
<goal>addSources</goal> | ||
<goal>addTestSources</goal> | ||
<goal>compile</goal> | ||
<goal>compileTests</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-failsafe-plugin</artifactId> | ||
<version>${maven-failsafe-plugin.version}</version> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.junit.platform</groupId> | ||
<artifactId>junit-platform-surefire-provider</artifactId> | ||
<version>${junit.platform.version}</version> | ||
</dependency> | ||
</dependencies> | ||
<executions> | ||
<execution> | ||
<id>junit5</id> | ||
<goals> | ||
<goal>integration-test</goal> | ||
<goal>verify</goal> | ||
</goals> | ||
<configuration> | ||
<includes> | ||
<include>**/*Test5.java</include> | ||
</includes> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
<version>2.20.1</version> | ||
<configuration> | ||
<useFile>false</useFile> | ||
<includes> | ||
<include>**/*Test.java</include> | ||
<include>**/*Spec.java</include> | ||
</includes> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
<repositories> | ||
<repository> | ||
<id>central</id> | ||
<url>http://jcenter.bintray.com</url> | ||
</repository> | ||
</repositories> | ||
|
||
<properties> | ||
<junit.platform.version>1.0.0</junit.platform.version> | ||
<groovy.version>2.5.6</groovy.version> | ||
<groovy-all.version>2.5.6</groovy-all.version> | ||
<groovy-sql.version>2.5.6</groovy-sql.version> | ||
<hsqldb.version>2.4.0</hsqldb.version> | ||
<spock-core.version>1.1-groovy-2.4</spock-core.version> | ||
<gmavenplus-plugin.version>1.6</gmavenplus-plugin.version> | ||
</properties> | ||
</project> | ||
|
148 changes: 148 additions & 0 deletions
148
core-groovy-collections/src/test/groovy/com/baeldung/map/MapTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package com.baeldung.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) | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.