Skip to content

Instantly share code, notes, and snippets.

@pgebert
Created April 5, 2023 19:58
Show Gist options
  • Save pgebert/738f075441f36ea877d301afc6894a59 to your computer and use it in GitHub Desktop.
Save pgebert/738f075441f36ea877d301afc6894a59 to your computer and use it in GitHub Desktop.
Implementation of treeMapOf and toTreeMap equivalent to mapOf and toMap in kotlin.
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