added component1() / component2() methods to Map.Entry along with a test case so that maps can be iterated over via: for ((k, v) in map)
This commit is contained in:
@@ -29,6 +29,16 @@ val <K,V> Map.Entry<K,V>.key : K
|
||||
val <K,V> Map.Entry<K,V>.value : V
|
||||
get() = getValue().sure()
|
||||
|
||||
/** Returns the key of the entry */
|
||||
fun <K,V> Map.Entry<K,V>.component1() : K {
|
||||
return getKey().sure()
|
||||
}
|
||||
|
||||
/** Returns the value of the entry */
|
||||
fun <K,V> Map.Entry<K,V>.component2() : V {
|
||||
return getValue().sure()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value for the given key or returns the result of the defaultValue function if there was no entry for the given key
|
||||
*
|
||||
|
||||
@@ -80,6 +80,23 @@ class MapTest {
|
||||
assertEquals("beverage,beer,location,Mells,name,James", list.makeString(","))
|
||||
}
|
||||
|
||||
test fun iterateWithExtraction() {
|
||||
val map = TreeMap<String, String>()
|
||||
map["beverage"] = "beer"
|
||||
map["location"] = "Mells"
|
||||
map["name"] = "James"
|
||||
|
||||
val list = arrayList<String>()
|
||||
for ((key, value) in map) {
|
||||
println("key = ${key}, value = ${value}")
|
||||
list.add(key)
|
||||
list.add(value)
|
||||
}
|
||||
|
||||
assertEquals(6, list.size())
|
||||
assertEquals("beverage,beer,location,Mells,name,James", list.makeString(","))
|
||||
}
|
||||
|
||||
test fun map() {
|
||||
val m1 = TreeMap<String, String>()
|
||||
m1["beverage"] = "beer"
|
||||
|
||||
Reference in New Issue
Block a user