How to make my save/load to/from file function more canonical? I  started to learn Kotlin and am now looking for common ways to save object to file and then load an object from a file. In my case, I am saving map&lt;String, List&lt;Int&gt;&gt; and have feeling that my approach is not good enough.

fun main(args: Array&lt;String&gt;) {
//    CreateDs.createLabeledList()
    val map = mutableMapOf&lt;String, List&lt;Int&gt;&gt;()
    map.put("first", listOf(1, 2, 3))
    map.put("second", listOf(4, 5, 6))
    val f = File("labels.txt")
    save(f, map)
    System.out.println(map)
    map.clear()
    load(f, map)
    System.out.println(map)


}

fun save(f: File, map: Map&lt;String, List&lt;Int&gt;&gt;) {
    f.printWriter().use { out -&gt;
        map.forEach {
            out.println("${it.key}:${it.value.toString().replace("[", "").replace("]", "").replace(" ", "")}")
        }
    }
}

fun load(f: File, map: MutableMap&lt;String, List&lt;Int&gt;&gt;) {
    f.inputStream().bufferedReader().useLines { lines -&gt;
        lines.forEach {
            val parts = it.split(":")
            map.put(parts[0], parts[1].split(",").map { it.toInt() })
        }
    }
}
Converting a list of Ints to a comma separated string

map.forEach {
    out.println("${it.key}:${it.value.toString().replace("[", "").replace("]", "").replace(" ", "")}")
}


This could be refactored as:

map.map { (key, value) -&gt;
    key + ":" + value.joinToString(",") { it.toString() }
}.forEach(out::println)


Loading data into the map

val map = mutableMapOf&lt;String, List&lt;Int&gt;&gt;()
map.put("first", listOf(1, 2, 3))
map.put("second", listOf(4, 5, 6))


This can be rewitten to use mapOf, which increases readability

val map = mutableMapOf(
        "first" to listOf(1, 2, 3),
        "second" to listOf(4, 5, 6)
)


Destructuring declaration for splitting string

val parts = it.split(":")
map.put(parts[0], parts[1].split(",").map { it.toInt() })


Using a destructuring declaration may make the purpose of this code clearer.

val (key, values) = "a".split(":")
map.put(key, values.split(",").map { it.toInt() })