Pair<A, B>
data structure stores a pair of related values, one of typeA
& another of typeB
.
Pair is used to store two related (associated) values. Examples of related values that can be stored using Pair :
PersonName → PersonDOB
ex.-Arjun → 12/12/12
Letter → NumberOfOccurrence
ex.-A → 20
Month → NumberOfDays
ex.-Jan → 31
Month → TotalExpense
ex.-Feb → 25000
Creating a Pair
Creating a Pair
You can create a Pair using either the Pair
constructor or the to
keyword :
Pair(/* one */, /* other */)
// OR
/* one */ to /* other */
Example :
fun main() {
val pair = Pair("Jan", 31) // Pair<String, Int>
// OR
val pair1 = "Jan" to 31
println(pair) // Prints (Jan, 31)
}