Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Triple in Kotlin

  • Last Updated : 25 Jan, 2021

In programming, we call functions to perform a particular task. The best thing about function is that we can call it any number of times and it returns some value after computation i.e. if we are having add() function then it always returns the sum of both the numbers entered. But, functions have some limitations like the function return only one value at a time. If there is a need to return more than one value of a different data types, then we can create a class and declare all the variables that we want to return from the function and after that create an object of the class and easily collect all the returned values in a list. The main problem is that if there are so many functions in the program which return more than one value at a time then we have to create a separate class for all those functions and finally use that class. This process increases the verbosity and complexity of the program. In order to deal with these types of problems, Kotlin introduced the concept of Pair and Triple

What is Triple? 

Kotlin language provides a simple datatype to store three values in a single instance. This can be done using a data class known as Triple. It is a simple generic class that stores any three values, there is no valuable meaning of the relationship between the three values. The comparison between two Triple objects is done on the basis of values, i.e. two Triples are equal only if all three components are equal. 

Class Definition: 

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

There are three parameters: 

  • A – type of the first value
  • B – type of the second value
  • C – type of the third value

Constructor

In Kotlin, the 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 Triple we use: 



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

Kotlin example of creating triple using the constructor: 

Kotlin




fun main() {
    val (x, y, z) = Triple(1, "Geeks", 2.0)
    println(x)
    println(y)
    println(z)
}

Output:  

1
Geeks
2.0

Properties

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

  • first: This field stores the first value of the Pair.
  • second: This field stores the second value of the Pair.
  • third: This field stores the third value of the Pair.

Kotlin program to retrieve the values of Triple using properties:

Kotlin




fun main() {
    // declare triple
    var triple = Triple("Hello Geeks",
                        "This is Kotlin tutorial",
                        listOf(10, 20, 30))
    println(triple.first)
    println(triple.second)
    println(triple.third)
}

Output: 

Hello Geeks
This is Kotlin tutorial
[10, 20, 30]

Functions

toString(): This function returns the string equivalent of the Triple. 



fun toString(): String

Kotlin program of using the function: 

Kotlin




fun main() {
    // first triple
    val triple = Triple(5, 5, 5)
    println("String representation is "+triple.toString())
     
      // second triple
    val triple2 = Triple("Geeks",
                         listOf("Praveen", "Gaurav", "Abhi"),
                         12345)
    print("Another string representation is "+ triple2.toString())
}

Output: 

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

Extension Functions

As we have learned in previous articles, extension functions provide the ability to add more functionality to the existing classes, without inheriting them. 

toList(): This function returns the List equivalent of the given Triple. 

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

Kotlin program of using the extended function:

Kotlin




fun main() {
    // first triple
    var obj = Triple(1, 2, 3)
    val list1: List<Any> = obj.toList()
    println(list1)
     
      // second triple
    var obj2 = Triple("Hello", 2.0000 ,
                      listOf(10, 20, 30))
    val list2: List<Any> = obj2.toList()
    println(list2)
}

Output: 

[1, 2, 3]
[Hello, 2.0, [10, 20, 30]]



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!