Pair in Kotlin (original) (raw)

Last Updated : 12 Jul, 2025

In programming, we often use functions to perform specific tasks. A great feature of functions is that we can call them as many times as we need, and they return a value after performing some computation. For example, an add() function will always return the sum of two numbers passed to it. However, there is a limitation a function can normally return only a single value at a time. But sometimes, we want to return multiple values of different data types from the same function. To do this, we could define a custom class, declare variables in it, and return an object of that class. This allows us to package multiple values together and return them as one. The problem with this approach is that if many functions in a program need to return multiple values, then we would have to create a new class for each such function which increases code complexity. To solve this, Kotlin provides **Pair and **Triple classes simple, built-in data types designed for holding two and three values respectively.

What is Pair?

In Kotlin, Pair is a generic class designed to hold exactly two values in a single object. These two values can be of the same type or different types. Kotlin’s Pair is a convenient way to group two related or unrelated objects together. Two Pair objects are considered equal if both their values are equal.

**Class Definition

data class Pair<out A, out B> : Serializable

There are **two parameters:

Constructor

In Kotlin, constructor is a special member function that is invoked when an object of the class is created primarily to initialize variables or properties. To create a new instance of the Pair we use:

Pair(first: A, second: B)

**Example:

Kotlin `

fun main() { val pair = Pair(1, "Geeks") println(pair.first) println(pair.second) }

`

**Output:

1
Geeks

Properties

We can either receive the values of pair in a single variable or we can use **first and **second properties to extract the values.

**Example:

Kotlin `

fun main() { val pair = Pair("Hello Geeks", "This is Kotlin tutorial")

println(pair.first)
println(pair.second)

}

`

**Output:

Hello Geeks
This is Kotlin tutorial

Functions of Pair

**toString():

The toString() function returns a string representation of the Pair.

fun toString(): String

**Example:

Kotlin `

fun main() { val pair1 = Pair(5, 5) val pair2 = Pair("Geeks", listOf("Praveen", "Gaurav", "Abhi"))

println("String representation is $pair1")
println("Another string representation is $pair2")

}

`

**Output:

String representation is (5, 5)
Another string representation is (Geeks, [Praveen, Gaurav, Abhi])

Extension Functions: toList()

Kotlin also provides an extension function to convert a Pair into a List. This function works only if both values in the Pair are of the same type.

fun Pair<T, T>.toList(): List

**Example:

Kotlin `

fun main() { val pair1 = Pair(1, 2) val pair2 = Pair("Hello", "Geeks")

println(pair1.toList())
println(pair2.toList())

}

`

**Output:

[1, 2]
[Hello, Geeks]

**Note: If the two values in the Pair are of different types, toList() cannot be used directly because the function requires both elements to be of the same type.