Created
April 5, 2023 19:58
-
-
Save pgebert/738f075441f36ea877d301afc6894a59 to your computer and use it in GitHub Desktop.
Implementation of treeMapOf and toTreeMap equivalent to mapOf and toMap in kotlin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.TreeMap | |
fun <K, V> treeMapOf(vararg pairs: Pair<K, V>): TreeMap<K, V> = | |
TreeMap<K, V>().apply { putAll(pairs) } | |
fun <K : Comparable<K>, V> Map<K, V>.toTreeMap(): TreeMap<K, V> = | |
TreeMap<K, V>().apply { putAll(this@toTreeMap) } | |
// Usage: | |
val mapA = treeMapOf(1 to "A") | |
val mapB = mapOf(1 to "A").toTreeMap() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment