diff --git a/libraries/stdlib/src/kotlin/JUtilMaps.kt b/libraries/stdlib/src/kotlin/JUtilMaps.kt index da6b1f9da11..22a6175fe3c 100644 --- a/libraries/stdlib/src/kotlin/JUtilMaps.kt +++ b/libraries/stdlib/src/kotlin/JUtilMaps.kt @@ -64,3 +64,14 @@ public inline fun java.util.Map.getOrPut(key: K, defaultValue: ()-> V return answer } } + + +/** + * Returns an [[Iterator]] over the entries in the [[Map]] + * + * @includeFunctionBody ../../test/MapTest.kt iterate + */ +public inline fun java.util.Map.iterator(): java.util.Iterator> { + val entrySet = this.entrySet()!! + return entrySet.iterator()!! +} \ No newline at end of file diff --git a/libraries/stdlib/test/MapTest.kt b/libraries/stdlib/test/MapTest.kt index 6df00479012..887ae86ade4 100644 --- a/libraries/stdlib/test/MapTest.kt +++ b/libraries/stdlib/test/MapTest.kt @@ -3,12 +3,11 @@ package test.collections import kotlin.test.* import java.util.* -import junit.framework.TestCase -import org.junit.Test +import org.junit.Test as test class MapTest { - Test fun getOrElse() { + test fun getOrElse() { val data = HashMap() val a = data.getOrElse("foo"){2} assertEquals(2, a) @@ -18,7 +17,7 @@ class MapTest { assertEquals(0, data.size()) } - Test fun getOrPut() { + test fun getOrPut() { val data = HashMap() val a = data.getOrPut("foo"){2} assertEquals(2, a) @@ -29,13 +28,13 @@ class MapTest { assertEquals(1, data.size()) } - Test fun sizeAndEmpty() { + test fun sizeAndEmpty() { val data = HashMap() assertTrue{ data.empty } assertEquals(data.size, 0) } - Test fun setViaIndexOperators() { + test fun setViaIndexOperators() { val map = HashMap() assertTrue{ map.empty } assertEquals(map.size, 0) @@ -46,4 +45,49 @@ class MapTest { assertEquals(map.size(), 1) assertEquals("James", map["name"]) } + + test fun iterate() { + val map = TreeMap() + map["beverage"] = "beer" + map["location"] = "Mells" + map["name"] = "James" + + var list = arrayList() + for (e in map) { + println("key = ${e.getKey()}, value = ${e.getValue()}") + + list.add(e.getKey()) + list.add(e.getValue()) +/* + // TODO compiler bug + list += e.getKey() + list += e.getValue() +*/ + } + + assertEquals(6, list.size()) + assertEquals("beverage,beer,location,Mells,name,James", list.makeString(",")) + } + + /* + TODO compiler bug + + test fun iteratorWithProperties() { + val map = TreeMap() + map["beverage"] = "beer" + map["location"] = "Mells" + map["name"] = "James" + + var list = arrayList() + for (e in map) { + println("got $e") + println("key = ${e.key}, value = ${e.value}") + list += e.key + list += e.value + } + + assertEquals(6, list.size()) + assertEquals("beverage,beer,location,Mells,name,James", list.makeString(",")) + } + */ }