Blog / August 17, 2018 / 3 mins read / By Suneet Agrawal

Pair and Triple in Kotlin

It is a very common use case where we want to return two values from a method, can be either of same data type or can be of different data types.

What usually we do there is either create some local variables if the method is of the same class and set those variables from the method and consume them in the place where needed or we create a class with just two variables and return that class object from the method.

This approach works fine but is it worth defining a new class every time when we return two value from a method.

What if we have several methods which return two values but each method returns the values of different data types? Are we going to create a separate class for each method?

The answer is NO.

Kotlin provides us with a predefined class Pair which can be used for the same. Pair is used to store or return two values of same or different data types. There can or cannot be any relation between both the values. They can be of the same or different data types.

And how do I use it?

Initialise it as a simple Pair class object by passing two values to the constructor.

Pair ("Hello", "How are you")
Pair ("First", 1)
Pair (10, null)
Pair (1.5, listOf(null))
var variable1 = "I am a String"
var variable2 = 10
    
Pair (variable1, variable2)

And how do I retrieve the values?

Either receive them in a single variable and use first and second properties or componentN method to extract the values, or receive in separate variables and use them directly.

var pair = Pair("Hello", "How are you")
println(pair.first)
println(pair.second)

println(pair.component1())
println(pair.component2())
//or
var (firstName, lastName) = Pair("Suneet", "Agrawal")
println(firstName)
println(lastName)

We can even use a normal method like toString() on this object.

val pair = Pair("I am a String", listOf(1,2,3))
print(pair.toString())

Or we can even call a copy method on a Pair object and change its values but while copying, the data type of the variables should be the same as the one we are trying to change the value of.

val pair = Pair("I am a String", listOf(1,2,3))
print(pair)
    
val anotherPair = pair.copy(first = "I am new String")
print(anotherPair)

We can even convert a Pair object to a list using toList() method and get the values using positions or perform other operations.

val pair = Pair("I am a String", 10)
val list = pair.toList()
    
println(list[0])
println(list.get(1))

Now What if I want to return three values from a method?

Simple, Use Triple class. It does the same functionality with three different properties.

It has the third property as third. We can retrieve the third value as either using third or by component3()

val triple = Triple("I am a String", listOf(1,2,3) , 10)
println(triple.third)
println(triple.component3())

It even supports copy and toList() method which has the same functionality as Pair but with three properties.

Last Question. What if I want to return four or more values?

Now you are getting greedy. Create your data class and return the object of that class.

Comments