Pair and Triple — Kotlin

Bharath
2 min readApr 22, 2022

Every time we write code we always use the function to perform certain actions. By default, functions return only one value. What if we want to return multiple values of different data types we wrap the values in class and return class.

Think of the situation If we already have a class with multiple values and we want to return two values, then what we do is call two methods sequentially. For eg, We have the class name Student in that class we store student information like name, department, age, address, and so on. If we want to print the name and address only then what we do is call the methods separately.

Its looks easier right. But we can make it even better. How?

Pair

Pair is a predefined class in Kotlin that is used to store and return two variables at a time. Those two variables can be of the same type or of a different type. So, whenever you want to return more than one variable from a function then you can use Pair in your function.

Triple

Triple is another predefined class in Kotlin. With the help of the Triple class in Kotlin, you can return 3 variables of the same or different type from one function. Also, you can use Triple class to store 3 variables of the same or different types.

For Triple, see the official document

Thanks and regards

--

--