Home > Net >  Difference Between Two List Elements in Kotlin
Difference Between Two List Elements in Kotlin

Time:08-12

I have a data class as below in Kotlin.

data class ProductData(
    val code: String,
    var value: Double)

There are two list for the above data class as

lstToday: List<ProductData> contains such as
("P1", 110)
("P2", 109)
("P3", 102)
("P4", 110)
..... 100  records

and

lstYesterday: List<ProductData> contains such as
("P1", 112)
("P2", 109)
("P3", 110)
("P4", 90)
..... 100  records

Both has the identical and exact number of records. The output I am looking for is as below.

Output 1: Difference between yesterday and today

lstDifference: List<ProductData> contains such as
("P1", -2)
("P2", 0)
("P3", 8)
("P4", -20)
..... 100  records

Output 2: Today Price and Difference between yesterday and today using the data class below.

data class ProductDisplayData(
    val code: String,
    var value: Double,
    var diff: Double

)

With list as below
lstDifference: List<ProductData> contains such as
("P1", 112, -2)
("P2", 109, 0)
("P3", 110, 8)
("P4", 90, -20)
..... 100  records

Can this be achieved using any functions Kotlin, or is it we have loop each element and get the result.

Thanks

CodePudding user response:

I would say this is not the most efficient solution. We're talking 0n ish. This could definitely be optimized. To note, this is fault tolerant of the code itself not being included in both days, and assumes 0 for a non-existent value between days. Trying to guarantee that both data sets will always be the same, is going to be more maintenance than writing code that will tolerate that mistake.

data class ProductData(
    val code: String,
    var value: Double
)

val dayOne = listOf(
    ProductData("P1", 110.0),
    ProductData("P2", 109.0),
    ProductData("P3", 102.0),
    ProductData("P4", 110.0),
    ProductData("P5", 105.0),
    ProductData("P6", 104.0),
    ProductData("P8", 32.0) // Not in set 2
)

val dayTwo = listOf(
    ProductData("P1", 110.0),
    ProductData("P2", 109.0),
    ProductData("P3", 102.0),
    ProductData("P4", 90.0),
    ProductData("P5", 49.0),
    ProductData("P6", 123.0),
    ProductData("P7", 239.0) // Not in set 1
)

fun periodDataDifference(dayOne: List<ProductData>, dayTwo: List<ProductData>): List<ProductData> {
    val mapOne = dayOne.associate { it.code to it.value }
    val mapTwo = dayTwo.associate { it.code to it.value }
    val keys = mapOne.keys   mapTwo.keys

    return keys.map { key ->
        val first = mapOne[key] ?: 0.0
        val second = mapTwo[key] ?: 0.0

        ProductData(key, second - first)
    }
}

val out = periodDataDifference(dayOne, dayTwo)

println(out)
/*
    [ProductData(code=P1, value=0.0), 
    ProductData(code=P2, value=0.0), 
    ProductData(code=P3, value=0.0), 
    ProductData(code=P4, value=-20.0), 
    ProductData(code=P5, value=-56.0), 
    ProductData(code=P6, value=19.0), 
    ProductData(code=P8, value=-32.0), // Set 1 only
    ProductData(code=P7, value=239.0)] // Set 2 only
 */

CodePudding user response:

If today and yesterday lists are gonna be same size this is the quickest solution I have

    val lstYesterday: List<ProductData> = listOf(
        ProductData("P1", 112.0),
        ProductData("P2", 109.0),
        ProductData("P3", 110.0)
    )
    val lstToday: List<ProductData> = listOf(
        ProductData("P1", 110.0),
        ProductData("P2", 109.0),
        ProductData("P3", 102.0)
    )
    val lstDifference: MutableList<ProductData> = mutableListOf()

    for ((index,j) in lstToday.withIndex()){
        val code = lstToday[index].code
        val value = lstToday[index].value-lstYesterday[index].value
        lstDifference.add(ProductData(code,value))
    }
  • Related