|
|||||||||||
| FRAMES NO FRAMES | |||||||||||
| SUMMARY: NESTED | PROPERTY | CONSTR | FUNCTION | DETAIL: PROPERTY | CONSTR | FUNCTION | ||||||||||
| Function Summary | ||
|---|---|---|
|
getOrElse(key: K, defaultValue: () -> V): V
Returns the value for the given key or returns the result of the defaultValue function if there was no entry for the given key |
|
|
getOrPut(key: K, defaultValue: () -> V): V
Returns the value for the given key or the result of the defaultValue function is put into the map for the given value and returned |
|
|
iterator(): Iterator<Entry<K, V>>
Returns an Iterator over the entries in the Map |
|
|
map(transform: (Entry<K, V>) -> R): List<R>
Returns a new List containing the results of applying the given transform function to each Map.Entry in this Map |
|
|
mapTo(result: C, transform: (Entry<K, V>) -> R): C
Transforms each Map.Entry in this Map with the given transform function and adds each return value to the given results collection |
|
|
mapValues(transform: (Entry<K, V>) -> R): Map<K, R>
Returns a new Map containing the results of applying the given transform function to each Map.Entry in this Map |
|
|
mapValuesTo(result: C, transform: (Entry<K, V>) -> R): C
Populates the given result Map with the value returned by applying the transform function on each Map.Entry in this Map |
|
|
orEmpty(): Map<K, V>
Returns the Map if its not null otherwise it returns the empty Map |
|
|
putAll(vararg values: #(K, V)): Unit
Puts all the entries into the map with the first value in the tuple being the key and the second the value |
|
|
set(key: K, value: V): V?
Provides [] access to maps |
|
|
toLinkedMap(): LinkedHashMap<K, V>
Converts this Map to a LinkedHashMap so future insertion orders are maintained |
|
|
toMap(map: Map<K, V>): Map<K, V>
Copies the entries in this Map to the given map |
|
|
toSortedMap(): SortedMap<K, V>
Converts this Map to a SortedMap so iteration order will be in key order |
|
|
toSortedMap(comparator: Comparator<K>): SortedMap<K, V>
Converts this Map to a SortedMap using the given comparator so that iteration order will be in the order defined by the comparator |
|
| Function Detail |
|---|
fun <K, V> Map<K, V>.getOrElse(key: K, defaultValue: () -> V): V
Returns the value for the given key or returns the result of the defaultValue function if there was no entry for the given key
val data = HashMap<String, Int>()
val a = data.getOrElse("foo"){2}
assertEquals(2, a)
val b = data.getOrElse("foo"){3}
assertEquals(3, b)
assertEquals(0, data.size())
fun <K, V> Map<K, V>.getOrPut(key: K, defaultValue: () -> V): V
Returns the value for the given key or the result of the defaultValue function is put into the map for the given value and returned
val data = HashMap<String, Int>()
val a = data.getOrElse("foo"){2}
assertEquals(2, a)
val b = data.getOrElse("foo"){3}
assertEquals(3, b)
assertEquals(0, data.size())
fun <K, V> Map<K, V>.iterator(): Iterator<Entry<K, V>>
Returns an Iterator over the entries in the Map
val map = TreeMap<String, String>()
map["beverage"] = "beer"
map["location"] = "Mells"
map["name"] = "James"
val list = arrayList<String>()
for (e in map) {
println("key = ${e.key}, value = ${e.value}")
list.add(e.key)
list.add(e.value)
}
assertEquals(6, list.size())
assertEquals("beverage,beer,location,Mells,name,James", list.makeString(","))
fun <K, V, R> Map<K, V>.map(transform: (Entry<K, V>) -> R): List<R>
Returns a new List containing the results of applying the given transform function to each Map.Entry in this Map
val data = arrayList("foo", "bar")
val lengths = data.map<String, Int>{ it.length }
assertTrue {
lengths.all{it == 3}
}
assertEquals(2, lengths.size)
assertEquals(arrayList(3, 3), lengths)
fun <K, V, R, C> Map<K, V>.mapTo(result: C, transform: (Entry<K, V>) -> R): C
Transforms each Map.Entry in this Map with the given transform function and
adds each return value to the given results collection
fun <K, V, R> Map<K, V>.mapValues(transform: (Entry<K, V>) -> R): Map<K, R>
Returns a new Map containing the results of applying the given transform function to each Map.Entry in this Map
val m1 = TreeMap<String, String>()
m1["beverage"] = "beer"
m1["location"] = "Mells"
val m2 = m1.mapValues<String,String,String>{ it.value + "2" }
println("Got new map $m2")
assertEquals(arrayList("beer2", "Mells2"), m2.values().toList())
fun <K, V, R, C> Map<K, V>.mapValuesTo(result: C, transform: (Entry<K, V>) -> R): C
Populates the given result Map with the value returned by applying the transform function on each Map.Entry in this Map
fun <K, V> Map<K, V>.orEmpty(): Map<K, V>
Returns the Map if its not null otherwise it returns the empty Map
fun <K, V> Map<K, V>.putAll(vararg values: #(K, V)): Unit
Puts all the entries into the map with the first value in the tuple being the key and the second the value
fun <K, V> Map<K, V>.toLinkedMap(): LinkedHashMap<K, V>
Converts this Map to a LinkedHashMap so future insertion orders are maintained
fun <K, V> Map<K, V>.toMap(map: Map<K, V>): Map<K, V>
Copies the entries in this Map to the given map
fun <K, V> Map<K, V>.toSortedMap(): SortedMap<K, V>
Converts this Map to a SortedMap so iteration order will be in key order
val map = hashMap<String,Int>(#("c", 3), #("b", 2), #("a", 1))
val sorted = map.toSortedMap<String,Int>()
assertEquals(1, sorted.get("a"))
assertEquals(2, sorted.get("b"))
assertEquals(3, sorted.get("c"))
assertEquals(arrayList("a", "b", "c"), sorted.keySet()!!.toList())
fun <K, V> Map<K, V>.toSortedMap(comparator: Comparator<K>): SortedMap<K, V>
Converts this Map to a SortedMap using the given comparator so that iteration order will be in the order
defined by the comparator
val map = hashMap(#("c", 3), #("bc", 2), #("bd", 4), #("abc", 1))
val c = comparator<String>{ a, b ->
val answer = a.length() - b.length()
if (answer == 0) a.compareTo(b) else answer
}
val sorted = map.toSortedMap(c)
assertEquals(arrayList("c", "bc", "bd", "abc"), sorted.keySet()!!.toList())
assertEquals(1, sorted.get("abc"))
assertEquals(2, sorted.get("bc"))
assertEquals(3, sorted.get("c"))
|
||||||||||
| FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | PROPERTY | CONSTR | FUNCTION | DETAIL: PROPERTY | CONSTR | FUNCTION | |||||||||