0% found this document useful (0 votes)
15 views3 pages

8 - Pair in Kotlin

8- Pair in Kotlin

Uploaded by

specsdeveloper13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views3 pages

8 - Pair in Kotlin

8- Pair in Kotlin

Uploaded by

specsdeveloper13
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Trending Now Data Structures Algorithms Topic-wise Practice Python Machine Learning Data Science JavaScript Java Web

cript Java Web Development Bootstrap

Pair in Kotlin
Read Discuss Courses Practice

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 return 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 has some limitations like function return only one value at a time. If there is need to return more than
one value of different data type, 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 returns 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 type of problems, Kotlin introduced the concept of Pair and Triple.

What is Pair? –

Kotlin language provides a simple datatype to store two values in a single instance. This can be done using a data
class known as Pair. It is a simple generic class that can store two values of same or different data types, and there
can or can not be a relationship between the two values. The comparison between two Pair objects is done on the
basis of values, i.e. two Pair objects are equal if their values are equal.

Class Definition –

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

There are two parameters:


A – type of the first value
B – type of the second value

Constructor –

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

Pair(first: A, second: B)
Kotlin example of creating pair using the constructor –

fun main() {
val (x, y) = Pair(1, "Geeks")
println(x)
println(y)
}

Output:

1
Geeks

Properties –

We can either receive the values of pair in a single variable or we can use first and second 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.

Kotlin program to retrieve the values of Pair using properties –

fun main() {
// declare pair
var pair = Pair("Hello Geeks", "This is Kotlin tutorial")
println(pair.first)
println(pair.second)
}

Output:

Hello Geeks
This is Kotlin tutorial

Functions –

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

fun toString(): String

Kotlin program of using the function –

fun main() {
val obj = Pair(5,5)
println("String representation is "+obj.toString())
val pair = Pair("Geeks", listOf("Praveen", "Gaurav", "Abhi"))
print("Another string representation is "+pair.toString())
}

Output:

String representation is (5, 5)


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

Extension Functions –
As we have learnt in previous articles, extension functions gives ability to add more functionality to the existing
classes, by without inheriting them.
toList(): This function returns the List equivalent of the given Pair.

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

Kotlin program of using the extended function –

fun main() {
// first pair
var obj = Pair(1,2)
val list1: List<Any> = obj.toList()
println(list1)
// second pair
var obj2 = Pair("Hello","Geeks")
val list2: List<Any> = obj2.toList()
println(list2)
}

Output

[1, 2]
[Hello, Geeks]

Last Updated : 02 Aug, 2019 8

Similar Reads
Android - Create Group BarChart with Kotlin Kotlin | Language for Android, now Official by
Google

Kotlin Data Types Hello World program in Kotlin

Android Shared Element Transition with Kotlin Kotlin | Retrieve Collection Parts

How to Create Expandable RecyclerView items in Destructuring Declarations in Kotlin


Android using Kotlin?

DatePicker in Kotlin Kotlin labelled continue

Previous Next

Triple in Kotlin Kotlin | apply vs with

Article Contributed By :
CharchitKapoor
C CharchitKapoor

You might also like