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:
James Strachan
2012-09-14 11:18:35 +01:00
parent 7ad0fd56f3
commit 81d83cd4f0
2 changed files with 27 additions and 0 deletions
+10
View File
@@ -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
*
+17
View File
@@ -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"