Home > front end >  How to continue using the value returned from getOrPut Map in Kotlin?
How to continue using the value returned from getOrPut Map in Kotlin?

Time:08-13

I am new to Kotlin and come from a JS background. I am trying to map through a list and perform some operations on them and return a list.

val rowValue=readHeader.map { header-> row.getOrPut(header) { "" } }

I am trying to do further operations on the value returned by this code block.

row.getOrPut(header) { "" } 

So, I am trying to do this:

       val rowValue=readHeader.map { header-> { val rowVal = row.getOrPut(header) { "" }
                if (rowVal.contains(","))
                {
                    "\"" "$rowVal" "\""
                }
                else{
                    rowVal
                }
            } }

The result is like so.

() -> kotlin.String!,() -> kotlin.String!

I am trying to loop through rowHeader which is a list of string and get the rowVal which is a map of key, value. And then check if the value is like apple,mango return \"apple,mango\" or return banana for all other case. How do I do that?

So, basically I am trying to use the value that I get in val rowVal = row.getOrPut(header) { "" } for further operations: eg: rowVal 1.

I have done this using forEach loop. How do I do this using a .map since the map returns a list?

        val rowData= emptyList<String>().toMutableList()
            readHeader.forEach { header->
                val value = row.getOrPut(header) { "" }
                if (value.contains(",") || value.contains(" ")){
                    rowData.add("\"${value}\"")
                }
                else{
                    rowData.add(value)
                }
            }

Expected input i,e

row =mapOf("firstName" to "John", "lastName" to "Doe", "fruits" to "apple,mango", "column" to "Value")
row1 =mapOf("firstName" to "Jane", "lastName" to "", "fruits" to "banana,grapes", "column" to "Value")

Expected output:

John,Doe,"apple,mango",Value
Jane,,"banana,grapes",Value

CodePudding user response:

Updated answer after OP added test-input in question.

The below code with tests should answer your question. Code is written inside a Kotest.

import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.data.forAll
import io.kotest.data.row
import io.kotest.matchers.shouldBe

class MyTest() : BehaviorSpec({

    given("StackOverflow question") {

        forAll(
            row(
                mapOf("firstName" to "John", "lastName" to "Doe", "fruits" to "apple,mango", "column" to "Value"),
                listOf("John", "Doe", "\"apple,mango\"", "Value")
            ),
            row(
                mapOf("firstName" to "Jane", "lastName" to "", "fruits" to "banana,grapes", "column" to "Value"),
                listOf("Jane", "", "\"banana,grapes\"", "Value")
            )
        ) { input, expectedResult ->

            `when`("mapping items $input, $expectedResult") {

                val result = input.values.map {
                    if (it.contains(",") || it.contains(" ")) {
                        "\"${it}\""
                    } else {
                        it
                    }
                }

                then("result should be as expected") {
                    result shouldBe expectedResult
                }
            }
        }
    }
})
  • Related