Implement a Multimap in Kotlin
A
Multimap is a map that allows mapping of a single key to multiple values. Kotlin does not provide any standard implementation of the Multimap class, but we can implement our own Multimap class in Kotlin.
Following is a simple implementation of the Multimap class in Kotlin using the HashMap and Collection class.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
class MultiMap<K, V> { private var map: MutableMap<K, MutableCollection<V>?> = HashMap() /** * Add the specified value with the specified key in this multimap. */ fun put(key: K, value: V) { if (map[key] == null) { map[key] = ArrayList() } map[key]!!.add(value) } /** * Associate the specified key with the given value if not * already associated with a value */ fun putIfAbsent(key: K, value: V) { if (map[key] == null) { map[key] = ArrayList() } // if the value is absent, insert it if (!map[key]!!.contains(value)) { map[key]!!.add(value) } } /** * Returns the Collection of values to which the specified key is mapped, * or null if this multimap contains no mapping for the key. */ operator fun get(key: Any?): Collection<V>? { return map[key] } /** * Returns a Set view of the keys contained in this multimap. */ fun keySet(): Set<K> { return map.keys } /** * Returns a Set view of the mappings contained in this multimap. */ fun entrySet(): Set<Map.Entry<K, Collection<V>?>> { return map.entries } /** * Returns a Collection view of Collection of the values present in * this multimap. */ fun values(): Collection<Collection<V>?> { return map.values } /** * Returns true if this multimap contains a mapping for the specified key. */ fun containsKey(key: Any?): Boolean { return map.containsKey(key) } /** * Removes the mapping for the specified key from this multimap if present * and returns the Collection of previous values associated with key, or * null if there was no mapping for key. */ fun remove(key: Any?): Collection<V>? { return map.remove(key) } /** * Returns the total number of key-value mappings in this multimap. */ fun size(): Int { var size = 0 for (value in map.values) { size += value!!.size } return size } /** * Returns true if this multimap contains no key-value mappings. */ val isEmpty: Boolean get() = map.isEmpty() /** * Removes all the mappings from this multimap. */ fun clear() { map.clear() } /** * Removes the entry for the specified key only if it is currently * mapped to the specified value and return true if removed */ fun remove(key: K, value: V): Boolean { return if (map[key] != null) map[key]!!.remove(value) else false } /** * Replaces the entry for the specified key only if currently * mapped to the specified value and return true if replaced */ fun replace(key: K, oldValue: V, newValue: V): Boolean { if (map[key] != null) { if (map[key]!!.remove(oldValue)) { return map[key]!!.add(newValue) } } return false } } fun main() { val multimap: MultiMap<String, String> = MultiMap() multimap.put("George", "Washington") multimap.put("George", "Bush") multimap.put("John", "Adams") multimap.put("John", "Tyler") multimap.put("John", "Kennedy") multimap.put("Zachary", "Taylor") println("----- Printing Multimap with keySet() function -----\n") for (lastName in multimap.keySet()) { println(lastName + ": " + multimap[lastName]) } } |
Output:
—– Printing Multimap with keySet() function —–
George: [Washington, Bush]
Zachary: [Taylor]
John: [Adams, Tyler, Kennedy]
That’s all about implementing a Multimap in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)