Pair and Triple in Kotlin

Meva Akkaya
Huawei Developers
Published in
3 min readDec 31, 2021
Let’s Learn!

In general, there is a limitation on functions that we use to perform a function, such that they only return one value at a time.If you want to return multiple values from different data types, there are many different methods. For example, you can create a class and declare all the variables you want to return from the function, and then create an object of the class and collect all the returned values in a list.
But what will you do if there are more functions that return multiple values?
Yes, then you need to create a separate class for all these functions and use that class at the end.

How troublesome and useless is that?

Let me introduce you to one of the easier methods, Kotlin - Pair and Triple!

We will examine how to use Pair and Triple, which has similar logic to Tuple (tuple deprecated for Kotlin) structure in other languages and how these data types enable to organize different values in one place.

Pair

Pair, as the name suggests, is a predefined Kotlin class used to store and return two variables at once. Let’s see how to use

Using

Class Definition:

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

Constructor:

Pair(first: A, second: B)

You can also use it with Pair by defining a variable before

Properties

To get the values stored in the Pair, we just need to call the first and second property of the Pair class.

or

Functions

to infix :

toString(): Used to convert variables in double to a string format

fun toString(): String

toList(): Used to convert variables in double to a List format

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

What if I have 3 variables? Here is Triple!

Triple

Again, as the name suggests, Triple is a predefined Kotlin class used to store and use three variables of the same or different type.

Using

Class Definition:

data class Triple<out A, out B, out C> : Serializable

Constructor

Triple(first: A, second: B, third: C)

Also like Pair, you can also use it with Triple by defining a variable before

Properties

we just need to call the first, second or third property of the Pair class.

Functions

toString(): returns the string equivalent of the Triple

fun toString(): String

toList(): returns the List equivalent of the Triple.

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

What if i need more than three variables?

Even if you want to return more than three variables, yes, you need to declare the variables by creating a data class in the way we know and use the variables by making them an object of that class.

Conclusion

In this article, I wanted to introduce the use of Pair and Triple as a convenience to many people’s classical approach. We learned how to use the Pair and Triple classes Kotlin provides. We have seen that if we want to return more than one variable at the same time, which is an alternative to tuple, we can use Pair in binary cases and Triple in triple cases.

Glad if it helped, thanks for reading!
Don’t forget to clap if you found this article helpful.

--

--