Skip to content

Instantly share code, notes, and snippets.

@chetdeva
Last active November 2, 2017 11:29
Show Gist options
  • Select an option

  • Save chetdeva/677dfbe733b594e07019509f7fbd7c5c to your computer and use it in GitHub Desktop.

Select an option

Save chetdeva/677dfbe733b594e07019509f7fbd7c5c to your computer and use it in GitHub Desktop.
package com.fueled.collapsingnotifications.util
/**
* Copyright (c) 2017 Fueled. All rights reserved.
*
* @author chetansachdeva on 22/09/17
*/
class MultiValuedMap<K, V> {
private val map: MutableMap<K, MutableList<V>>
init {
this.map = HashMap()
}
fun put(key: K, value: V) {
if (map.containsKey(key)) {
map[key]?.add(value)
} else {
val values = ArrayList<V>()
values.add(value)
map.put(key, values)
}
}
fun putValues(key: K, values: List<V>) {
for (i in values.indices) {
put(key, values[i])
}
}
fun getValues(key: K): List<V>? {
return if (map.containsKey(key)) {
map[key]
} else null
}
fun removeValues(key: K, values: List<V>) {
for (i in values.indices) {
remove(key, values[i])
}
}
fun remove(key: K, value: V) {
if (map.containsKey(key)) {
map[key]?.remove(value)
}
}
override fun toString(): String {
var s = ""
for ((key, value) in map) {
for (i in 0..value.size - 1) {
s += key.toString() + " " + value.toString()
}
}
return s
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment