Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

491
Visualizações
Merge nested maps with similar outer keys, keeping both inner map's key value pairs

I would like to merge two nested maps as per below, without overwriting the data of the outer map's values, or by replacing the inner map, and overwriting the data already there.

Ideally within the nested map, I would keep the key/value pairs I already have, and add to that with the second map.

    val mapOne: MutableMap<String, MutableMap<String, String>>  = mutableMapOf(
        "DirectoryOne" to  mutableMapOf(
            "FeatureOne" to "SomeUniqueStringFeature1",
            "FeatureTwo" to "SomeUniqueStringFeature2"
        )
    )
    val mapTwo: MutableMap<String, MutableMap<String, String>> = mutableMapOf(
        "DirectoryOne" to  mutableMapOf(
            "FeatureThree" to "SomeUniqueStringFeature3"
        )
    )

The result I need would look as follows:

    val mapOne: MutableMap<String, MutableMap<String, String>>  = mutableMapOf(
        "DirectoryOne" to  mutableMapOf(
            "FeatureOne" to "SomeUniqueStringFeature1",
            "FeatureTwo" to "SomeUniqueStringFeature2",
            "FeatureThree" to "SomeUniqueStringFeature3"
        )
    )

Just to elaborate some into reasoning behind this. I am planning on building the map dynamically at runtime and add features to a single map as they are required to be added throughout the duration of the application.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I was able to perform the desired action with the below code. Iterating over the outer key/value pairs and merging the nested key/value pairs with a BiConsumer/BiFunction to mapOne. (Typing got a bit burdensome)

mapTwo.forEach(BiConsumer { k: String, v: MutableMap<String, String> ->
        mapOne.merge(k, v,
            BiFunction { v1: MutableMap<String, String>, v2: MutableMap<String, String> ->
                v1.putAll(v2)
                v1
            })
    })

I soon discovered that I could more easily employ a lambda (Removing the BiConsumer/BiFunction) and then move the lambda out of the merge parenthesis. The final optimal solution is shown below.

mapTwo.forEach { (k: String, v: MutableMap<String, String>) ->
        mapOne.merge(k, v
        ) { v1: MutableMap<String, String>, v2: MutableMap<String, String> ->
            v1.putAll(v2)
            v1
        }
    }

I am wrapping the above snippet in a function/method to be able to apply this multiple times where needed.

over 4 years ago · Santiago Trujillo Relatório

0

If you want to avoid the awkward use of the Java 8 merge function, you can use getOrPut. This is also more efficient because of the function inlining.

Also, according to Kotlin coding conventions, it's preferable to use a regular for loop instead of forEach (unless you're calling it on a nullable or at the end of a chain of collection operators).

for ((key, map) in mapTwo) {
    mapOne.getOrPut(key, ::mutableMapOf).putAll(map)
}

Generalized:

fun <K1, K2, V> MutableMap<in K1, MutableMap<K2, V>>.mergeIn(other: Map<out K1, Map<out K2, V>>) {
    for ((key, map) in other) {
        getOrPut(key, ::mutableMapOf).putAll(map)
    }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda