In this tutorial you will learn about Pair And Triple via simple step by step examples.

Example 1: Pair AndT riple

Let us look at a simple Pair And Triple example written in Kotlin Programming Language. Follow the following steps:

Step 1: Create Project

  1. Open your favorite Kotlin IDE.
  2. In the menu go to File --> Create New Project.

Step 2: Add Dependencies

No dependencies are needed for this project.

Step 3: Write Code

Our code will comprise the following Kotlin files:

  • PairAndTriple.kt
  1. In your editor or IDE, create a file known as PairAndTriple.kt.
  2. Then add the following code:

(a). PairAndTriple.kt

package Objects.PairAndTriple

//In the old days (Kotlin M2), Kotlin has tuples. They have been removed at M3. Now you can use instead Pair or Triple (they are implemented as data class)
fun main(Args : Array<String>) {
    var greet = Pair("Hello", "World")
    val(word1, word2) = greet

    println("${greet.first} ${greet.second} or $word1 $word2")

    var greet2 = Triple("I", "am", 34)
    val(word3, word4, number) = greet2

    println("${greet2.first} ${greet2.second} ${greet2.third} or $word3 $word4 $number")

    //extension functions "to" creates pair
    val greet3 = "Hello" to "World2"
    println ("${greet3.first} ${greet3.second}")
}

Step 4: Run

Copy the code, build and run.

Reference

Here are the reference links:

Number Link
1. Download Example
2. Follow code author
3. Code: Apache 2.0 License