RKVM
RKVM

Reputation: 87

How to use "Pair" in Kotlin dataclass

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.

Upvotes: 0

Views: 605

Answers (2)

Filip Petrovski
Filip Petrovski

Reputation: 444

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) 

Upvotes: 1

Saurav Rao
Saurav Rao

Reputation: 684

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

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

Upvotes: 1

Related Questions