Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Kotlin to use groupby multiple fields and adding values

I am trying to calculate the total costs by grouping multiple items using Kotlin’s built in method

I have a data structure like following;

Data(id=1, cost=1, env='prod', item='Storage')
Data(id=2, cost=2, env='qa', item='Storage')
Data(id=3, cost=3, env='prod', item='Storage')

Data(id=4, cost=5, env='qa', item='Bandwidth')
Data(id=5, cost=2, env='qa', item='Bandwidth')
Data(id=6, cost=5, env='prod', item='Bandwidth')
Data(id=7, cost=6, env='prod', item='Bandwidth')
Data(id=8, cost=1, env='prod', item='Bandwidth')

Data(id=9, cost=5, env='qa', item='vm')
Data(id=10, cost=2, env='uat', item='vm')
Data(id=11, cost=3, env='qa', item='vm')
Data(id=12, cost=4, env='prod', item='vm')
Data(id=13, cost=5, env='uat', item='vm')

I would like to add the cost using "item" and "env"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

So expected end result will be

data class TotalCost(val item: String, val env: String, val cost: Double){}


{
    ("Storage", "prod", 4)
    ("Storage", "qa", 2)
    
    ("Bandwidth", "qa", 7)
    ("Bandwith", "prod", 12)
    
    ("vm","qa", 8)
    ("vm", "uat", 7
    ("vm", "prod", 4)
}

I tried using groupBy but couldn’t get the result i want.

>Solution :

data class Data(
  val id: Int,
  val cost: Int,
  val env: String,
  val item: String
)

data class TotalCost(
  val item: String,
  val env: String,
  val cost: Double
)

val input = listOf(
  Data(id = 1, cost = 1, env = "prod", item = "Storage"),
  Data(id = 2, cost = 2, env = "qa", item = "Storage"),
  Data(id = 3, cost = 3, env = "prod", item = "Storage"),

  Data(id = 4, cost = 5, env = "qa", item = "Bandwidth"),
  Data(id = 5, cost = 2, env = "qa", item = "Bandwidth"),
  Data(id = 6, cost = 5, env = "prod", item = "Bandwidth"),
  Data(id = 7, cost = 6, env = "prod", item = "Bandwidth"),
  Data(id = 8, cost = 1, env = "prod", item = "Bandwidth"),

  Data(id = 9, cost = 5, env = "qa", item = "vm"),
  Data(id = 10, cost = 2, env = "uat", item = "vm"),
  Data(id = 11, cost = 3, env = "qa", item = "vm"),
  Data(id = 12, cost = 4, env = "prod", item = "vm"),
  Data(id = 13, cost = 5, env = "uat", item = "vm")
)

val result = input
  .groupBy { 
    it.item to it.env 
  }
  .map { (key, value) ->
    listOf(
      key.first,
      key.second,
      value.sumOf { it.cost }
    )
  }

result.forEach(::println)

Output:

[Storage, prod, 4]
[Storage, qa, 2]
[Bandwidth, qa, 7]
[Bandwidth, prod, 12]
[vm, qa, 8]
[vm, uat, 7]
[vm, prod, 4]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading