Home > Enterprise >  How to use "Pair" in Kotlin dataclass
How to use "Pair" in Kotlin dataclass

Time:01-18

I have variableA and variableB. I would like to implement Arraylist(pair<variableA, variableB>) in Kotlin data class. Ofcourse, I can use Arraylist of another class contains variableA and variableB.

data class Database(
    val rfHandler: ArrayList<RfHandler> = ArrayList(),
    val btThread: ArrayList<BTThread> = ArrayList(),
)

It would be beter if some could suggest other better way if any.

CodePudding user response:

Did you try using Pair data structure? (One in AndroidX Pair and other in Kotlin Pair)

val pairlist = ArrayList<Pair<RfHandler, BTThread>>()

CodePudding user response:

You could construct your class as:

data class Database(val list: ArrayList<Pair<RfHandler, BTThread>>)

Or if you would like to make your class itself be a pair, then you could extend it like so:

data class Database(
    val rfHandler: ArrayList<RfHandler> = ArrayList(),
    val btThread: ArrayList<BTThread> = ArrayList(),
): Pair<ArrayList<RfHandler>, ArrayList<BTThread>>(rfHandler, btThread) 
  •  Tags:  
  • Related