|
| 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