티스토리 뷰

공부

[kotlin] map -> flatMap

승가비 2022. 8. 11. 16:36
728x90
    fun flatMap(
        map: Map<String, Any>,
        flat: MutableMap<String, Any>? = mutableMapOf(),
        prefix: String? = null
    ): Map<String, Any> {
        flat!!

        for (k in map.keys) {
            val key = if (prefix == null) {
                k
            } else {
                "$prefix.$k"
            }

            if (map[k] is Map<*, *>) {
                val m = map[k] as Map<String, Any>

                flatMap(m, flat, key)
                continue
            }

            flat[key] = map[k]!!
        }

        return flat
    }
728x90
댓글
댓글쓰기 폼