Updated April 10, 2023
Introduction to Kotlin Tuple
The kotlin tuple is defined as one of the type and it is default method used for to calculate the expression and it is no longer supported from the kotlin library it uses data classes and other library classes like pair and triple it supports more similar to the kotlin application the present or upgraded application will support or migrated automatically the structure does not similar to the tree format and it does not have the library analogs also it does not supported with some kotlin library classes using the Alt+Enter key to replace the tuples with the specific library classes. So now the tuples are deprecated on the kotlin library.
Syntax
In kotlin language many default keywords, variables, classes and functions for to implement the android based application with more sophisticated. The tuple with 2 elements like pair and triple is the alias for the three tuple elements.
fun main(args:Array<String>)
{
val variable name=Tuple(""); // it’s a old one and not being used in the current kotlin library it is a destructive class
val obj = Triple("")
----- some kotlin codes depends upon the requirement---
}
The above codes are the basic syntax for created and utilised the tuple type in the kotlin application. It has pairs and triple feature the pair is exhibited with the value semantics if the pair value has more than once then it will validate the condition based on their requirement.
How does Tuple work in Kotlin?
The tuple method is one of the dereferenced techniques that has to be used in the component elements the data elements and classes are the most preferred way of dealing the tuple like data structures the library classes are pair and triple are available for the respective purposes. The destructing declaration works on the concept of the tuple and other component() based function so that generally kotlin does not support the tuples that brings a lot of tuples added with some merits and demerits. It is one of the dropped feature instead of that the pair and tuples will be added and it is a very common case that has to be returned with the two set of values from the default method and it can be of either same data type or can be of different data types we can use either single variable or multiple variable for to retrieve the values in the console from backend to UI console. If we can use more than one variable as a parameter that can be of the triple class it does have the same functionality with other different type properties. The other methods like copy and toList() method has the same behaviour like pair concept with include the three properties.
Examples of Kotlin Tuple
Examples of kotlin tuple are given below:
Example #1
Code:
package one;
fun main(args: Array<String>)
{
println("Welcome To My Domain have a nice day users the tuple concept which is implemented with the pair and triple concepts please keep on spenting time with our application")
var firsts = Triple("This is the first input", "This is the second input","This is the third input")
val result1: List<Any> = firsts.toList()
println(result1)
var second = Triple("First input is the string based input", 3.4567 ,
listOf(1234, "We passed the string inputs on the second placeholder", 3456778))
val result2: List<Any> = second.toList()
println(result2)
println("Thank you users your kotlin triple login codes which based on the tuple concept and the net output result is : " + result2)
}
Output:
The above example we used triple concept for to use more than one values with the single instance for to implement the tuple concept in the application.
Example #2
Code:
package one;
fun main(args: Array<String>)
{
println("Welcome To My Domain have a nice day users the tuple concept which is implemented with the pair and triple concepts please keep on spenting time with our application")
var first = Pair("Its the first input which based on the string format",2234)
val result1: List<Any> = first.toList()
println(result1)
var second = Pair("Thank you users your first string based input","Its the second string inputs")
val result2: List<Any> = second.toList()
println(result2)
println("The kotlin language provides simple datatype for to store two values in the single instance with the help of Pair concepts : " + result2)
val third=Pair("Employee Id", "Employee Name")
val result3: List<Any> = third.toList()
println(result3)
val four=Pair("Employee Address", "Employee mobile")
val result4: List<Any> = four.toList()
println(result4)
val five= Pair("Your string inputs",2021)
val result5: List<Any> = five.toList()
println(result5)
println("Thank you users your kotlin triple login codes which based on the tuple concept and the net output result is : " + result5)
}
Output:
The second example we used Pairs() concept for to implement the tuple features in the application.
Example #3
Code:
package one;
fun example(): Pair<Int, String> {
val eg = (1..37).shuffled().first()
if (eg % 2 == 0) {
return Pair(1, "Your inputs are validated successfully")
} else {
return Pair(0, "Sorry your inputs are failed to get validated please enter correct inputs")
}
}
fun main(args: Array<String>)
{
println("Welcome To My Domain have a nice day users the tuple concept which is implemented with the pair and triple concepts please keep on spenting time with our application")
val (dem1, dem2) = example()
println("Result is $dem1, $dem2")
var eg2 = Pair("This is the first input", "This is the second input?")
println(eg2.first)
println(eg2.second)
println(eg2.component1())
println(eg2.component2())
val demo = Pair("13564", 27869)
val demo1 = Pair("Kotlin Tuple, Triple and Pairs", 1423)
val (x, y) = Pair("This is the Third Exampe", 2021)
println("$x, $y")
val (dt, dt1) = Pair("Thank you users", listOf(32,21,13))
println("$dt, $dt1")
var firsts = Triple("This is the first input", "This is the second input","This is the third input")
val result1: List<Any> = firsts.toList()
println(result1)
println("Thank you users your kotlin triple login codes which based on the tuple concept and the net output result is : " + eg2)
}
Output:
In the final example, we used both pairs and triple concepts for to store more than one value with a single instance.
Conclusion
The pairs and triple is the alternate and latest concept for to implement the kotlin tuples in the application. In android based application the kotlin features and other default packages are used for to implement the application the code redundancy are the main benefit for the kotlin language when compare to the java based application.
Recommended Articles
This is a guide to Kotlin Tuple. Here we discuss the introduction, syntax, and working of tuple in kotlin along with different examples and its code implementation. You may also have a look at the following articles to learn more –