Updated April 4, 2023
Introduction to Kotlin pair
Kotlin pair is one of the datatypes and it is provided with the simple datatype to store two values in a single instance and this can be done using the data classes and it is also a simple generic class that contains two values of the same or different data types and there can or cannot be the relationship between the two values and the comparison between the two pairs objects is done on the basis of the values which is compared from the other datatype values and also the two pair objects are equal if the values are in equal.
Syntax
In kotlin language has many default keywords, variables and functions for to implement the applications. Among that datatype is the main role for declaring and store the values in the memory using the variable.
fun main()
{
val (vars1, vars2) = Pair(value1, value2)
}
The above codes are the basic syntax for implementing the kotlin pair type and it is the simple datatype to store the two separate values in a single instance. This can be achieved used only through the Pair hence it is the generic class that can be store with two values of same and different datatype it cannot be a relationship between the two values.
How do pair work in Kotlin?
Generally, kotlin language will call default functions to perform the user operations in the specific task based on the requirement. The best thing of the kotlin functions is that we can able to call it any number of times and utilised with the object instance. It also return some values after the computation hence we are adding the default functions like add(), run(), etc. And also each function has default limits for accessing the values at any time and is there needed to return with more than the value of the different datatype values. This kotlin pair container to ease passing around the tuple of two objects and also this object provides the sensible implementation of equals() method and it returns around the tuple of two objects. It is very common to use case and where we want to return with two pair values can be of the method with either of the same and different data types. The kotlin pair will return with any number of values and also it accepts more than one return value at a time. Using data class it returns the values from the functions and using pair and tuple class also using array values for possible to return the pair values in kotlin.
Example #1
Code:
fun main() {
println("Welcome To My Domain its the first example that related to the kotlin pair")
val (a, b) = Pair(1, "Siva is the fXY Company chairman")
println(a)
println(b)
val (a1, b1) = Pair(2, "Raman is the abc Company chairman and it is the founder. His early life was more struggle in his carrier")
println(a1)
println(b1)
val (a2,b2) = Pair(3, "Arun is the re Company chairman and he is the Researcher for computer science programming language")
println(a2)
println(b2)
val (a3, b3) = Pair(4, "Kumar is the trwe Company chairman and he is also one of the worker in this company due to his performance he is promoted to the next chairman")
println(a3)
println(b3)
val (a4, b4) = Pair(5, "Arun kumar is the eqq Company chairman and he is the programmer to implement some web based applications, and he is developed apps that improve hi company efficiency in next level")
println(a4)
println(b4)
val (a5, b5) = Pair(6, "XX is the ttre Company chairman and he is one of the hacker to know all the basic hacking techniques for to protect the applications")
println(a5)
println(b5)
val (a6, b6) = Pair(7, "OIu is the employee of the fXY Company and he is one of the talented person for to developing the applications and improve the company efficiency")
println(a6)
println(b6)
val (a7, b7) = Pair(8, "poi is the rewe Company chairman and the formal employee of the facebook company and he is the very talented person for to develop the company growth")
println(a7)
println(b7)
val (a8, b8) = Pair(9, "erte is the qwq Company chairman and the formal employee of the whatsapp company and he is the very talented person for to develop the company growth")
println(a8)
println(b8)
val (a9, b9) = Pair(10, "poi is the poi Company chairman and the formal employee of the Facebook company and he is the very talented person for to develop the company growth")
println(a9)
println(b9)
val (a10, b10) = Pair(11, "poi is the nmnb Company chairman and the formal employee of the ramngropus company and he is the very talented person for to develop the company growth")
println(a10)
println(b10)
}
Output:
The above example is the basic example for utilizing the kotlin pairs in the script.
Example #2
Code:
fun main() {
var a = Pair(1989,1990)
val lst: List<Any> = a.toList()
println(lst)
var b = Pair("This is the August Month","It is the Audi Tamil month")
val lst1: List<Any> = b.toList()
println(lst1)
var c = Pair(254,"Welcome To My Domain its the second example that related to the kotlin Pair concepts")
val lst2: List<Any> = c.toList()
println(lst2)
}
Output:
The second example is related to the kotlin list with pairs. We can call and use the pair values in the collection.
Example #3
fun main()
{
val x = Pair("Your first input is string type", listOf(1,2,3))
print(x)
val y = x.copy(first = "Welcome To My Domain its the third example that related to the kotlin pair concepts")
print(y)
val z= y.copy(first = "Have a Nice day users")
println(z)
}
Output:
The final example that related to kotlin Pair and also we can able to copy the pair values to another variable.
Conclusion
In kotlin language, we have used many concepts and features to implement the application. Like that we can call and utilized the Pair type values in the script. Mainly the kotlin pair is used to store and return the two values of the same or different data types.
Recommended Articles
This is a guide to Kotlin pair. Here we discuss the Introduction, syntax, and working of pair in Kotlin along with different examples for better understanding. You may also have a look at the following articles to learn more –