kotlin
Extensions on java.util.Map

extension functions on class java.util.Map
from package kotlin

Function Summary
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
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
fun <K, V>
Map<K, V>
iterator(): Iterator<Entry<K, V>>
Returns an Iterator over the entries in the Map
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
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
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>
set(key: K, value: V): V?
Provides [] access to maps
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
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
 

Function Detail
source

getOrElse


 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

source
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())
source

getOrPut


 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

source
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())
source

iterator


 fun <K, V> Map<K, V>.iterator(): Iterator<Entry<K, V>>

Returns an Iterator over the entries in the Map

source
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(","))
source

map


 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

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

mapTo


 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

source

mapValues


 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

source
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())
source

mapValuesTo


 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

source

orEmpty


 fun <K, V> Map<K, V>.orEmpty(): Map<K, V>

Returns the Map if its not null otherwise it returns the empty Map

source

putAll


 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

source

set


 fun <K, V> Map<K, V>.set(key: K, value: V): V?

Provides [] access to maps

source

toLinkedMap


 fun <K, V> Map<K, V>.toLinkedMap(): LinkedHashMap<K, V>

Converts this Map to a LinkedHashMap so future insertion orders are maintained

source

toMap


 fun <K, V> Map<K, V>.toMap(map: Map<K, V>): Map<K, V>

Copies the entries in this Map to the given map

source

toSortedMap


 fun <K, V> Map<K, V>.toSortedMap(): SortedMap<K, V>

Converts this Map to a SortedMap so iteration order will be in key order

source
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())
source

toSortedMap


 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

source
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"))


Copyright © 2010-2012. All Rights Reserved.