Kotlin triple

Kotlin triple DEFAULT

Triple in Kotlin

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

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

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

 

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

 

Output: 

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


Sours: https://www.geeksforgeeks.org/triple-in-kotlin/

Pair and Triple in Kotlin

Pair and Triple in Kotlin

In any programming language, we use functions to perform a particular activity. For eg., if you want to add the details of students of a college then instead of writing the same lines of code a number of times, you can use the concept of a function . And after that, you can call that function as many times as you want. Also, the best part of the function is that a function can return some value i.e. if you are having a function to add two numbers then the return value of that function can be a number of integer type having the sum of both the entered numbers.

But functions has some limitations also(when used normally). Think of a situation, where you want the name and age of a particular student of a college, then if you want to use the function approach then you can return only one value at a time. If you want to return more than one value for different data type (in our example, name is of string type, while age is of type integer), then you have to make use of dummy class where you can declare all the variables that you want to return from the function and after that make an object of the class and collect the returned value in some List.

But the problem here is that if you are having so many things to do i.e. so many functions to work on which returns more than one value then you have to make a separate class for all those functions and finally use that class. This will be a very complex and lengthy approach.

To deal with these type of situations, Kotlin introduced the concept of Pair and Triple. With the help of Pair and Triple you can return more than one value(two in case of pair and three in case of the triple) of different data types.

So, in this blog, we will learn how to use pairs and triples in Kotlin. So, let’s get started.

Pair

Pair is a predefined class in Kotlin that is used to store and return two variables at a time. Those two variables can be of the same type or of a different type. So, whenever you want to return more than one variable from a function then you can use Pair in your function. But how to use Pair? Let’s have a look at how to use Pair in Kotlin:

You can use pair as described above. Also you can define some variable and then pass that variable in the Pair. Following example shows the same:

Using first and second property

In order to retrieve the values stored in the Pair, we can use the first and second property of the Pair class:

Also, you can assign the variables of the Pair to another variable and use those variables like below:

Infix

In kotlin, we can use easily destructure the variable declaration using to infix function.

and declare the getWebsite() function as,

toString() function

toString() is a function that is used to convert the variables of the pair into string and use that variables as string.

toList() function

toList() function converts the Pair variable in form of List i.e. you can use the Pair variables just like List items.

Triple

Triple is another predefined class in Kotlin. With the help of Triple class in Kotlin, you can return 3 variables of same or different type from one function. Also, you can use Triple class to store 3 variables of same or different type.

The use of Triple is the same as that of Pair in Kotlin. Following example shows the use of Triple in Kotlin:

Also, there are functions like toString() and toList(), that can be used in Triple also, in the same manner as used in the Pair. Following example shows the use of toString() and toList() in Triple:

Summary

So, in this blog, we learned how to use the Pair and Triple class in Kotlin. We saw that if we want to return more than one variable from the same function then we can use Pair(in case of two variables) and Triple(in case of three variables). Also, the returned variables of the Pair and Triple class be of same type or different type. If you want to return more than three variables then you have to still apply the same approach as used before i.e. create a class, declare the variables and use the variables by making an object of that class.

So, that’s it for this blog.

Keep Learning :)

Team MindOrks!

Sours: https://blog.mindorks.com/pair-and-triple-in-kotlin
  1. Serendipity sea monster
  2. 3d exercises
  3. Smb account balance

Kotlin Tuple

Kotlin Tuple

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.

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 Tuple works 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:

Output:

Kotlin Tuple-1

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:

Output:

Kotlin Tuple-2

The second example we used Pairs() concept for to implement the tuple features in the application.

Example #3

Code:

Output:

Kotlin Tuple-3

The final example we used both pairs and triple concept for to store more than one values 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 also discuss the definition and how tuple works in kotlin? along with different examples and its code implementation. You may also have a look at the following articles to learn more –

  1. Scala Tuples
  2. Kotlin vs Swift
  3. Kotlin Range
  4. Kotlin Loops
Sours: https://www.educba.com/kotlin-tuple/
#6 Pairs and Triples: Kotlin Android Tutorial

Pair and Triple in Kotlin


It is a very common use case where we want to return two values from a method, can be either of same data type or can be of different data types.

What usually we do there is either create some local variables if the method is of the same class and set those variables from the method and consume them in the place where needed or we create a class with just two variables and return that class object from the method.

This approach works fine but is it worth defining a new class every time when we return two value from a method.
What if we have several methods which return two values but each method returns the values of different data types?
Are we going to create a separate class for each method?

The answer is NO.


Kotlin provides us with a predefined class which can be used for the same. is used to store or return two values of same or different data types. There can or cannot be any relation between both the values. They can be of the same or different data types.

And how do I use it?

Initialise it as a simple class object by passing two values to the constructor.


Pair ("Hello", "How are you")
Pair ("First", 1)
Pair (10, null)
Pair (1.5, listOf(null))
var variable1 = "I am a String"
var variable2 = 10

Pair (variable1, variable2)

And how do I retrieve the values?

Either receive them in a single variable and use and properties or method to extract the values, or receive in separate variables and use them directly.


var pair = Pair("Hello", "How are you")
println(pair.first)
println(pair.second)

println(pair.component1())
println(pair.component2())
//or
var (firstName, lastName) = Pair("Suneet", "Agrawal")
println(firstName)
println(lastName)


We can even use a normal method like on this object.


val pair = Pair("I am a String", listOf(1,2,3))
print(pair.toString())

Or we can even call a method on a object and change its values but while copying, the data type of the variables should be the same as the one we are trying to change the value of.


val pair = Pair("I am a String", listOf(1,2,3))
print(pair)

val anotherPair = pair.copy(first = "I am new String")
print(anotherPair)


We can even convert a object to a list using method and get the values using positions or perform other operations.


val pair = Pair("I am a String", 10)
val list = pair.toList()

println(list[0])
println(list.get(1))

Now What if I want to return three values from a method?

Simple, Use class. It does the same functionality with three different properties.

It has the third property as . We can retrieve the third value as either using or by


val triple = Triple("I am a String", listOf(1,2,3) , 10)
println(triple.third)
println(triple.component3())

It even supports and method which has the same functionality as but with three properties.


Last Question. What if I want to return four or more values?

Now you are getting greedy. Create your data class and return the object of that class.

Reference: Pair and Triple

Sours: https://agrawalsuneet.github.io/blogs/pair-and-triple-in-kotlin/

Triple kotlin

Why are you, Dima, not undressing. Take off your clothes, rather, we will consider you, otherwise Svetka praised your body so much, praised it so much, it even became enviable. She said that you have such an athletic body one feast for the eyes.

Top 4 Dying Programming Languages of 2019 - by Clever Programmer

I dont think its worth worrying. Great, thanks. Can you show me the direction and the name. I'll take you if you don't mind, "he offered gallantly. " I also eat there, excellent cuisine.

Similar news:

If you have already taken it, I would have warned you. At least I could go in the morning and withdraw from the card. Okay, come on, let's think of something now - Olya put down the phone I stood in the corridor and listened attentively to. Their conversation. The girl came up to me and smiled, said in a soft voice Sorry, we dont have any money now, maybe youll.



299 300 301 302 303