-
Notifications
You must be signed in to change notification settings - Fork 22
Kotlin Basics : What are pairs and triplets
Devrath edited this page Jan 1, 2024
·
2 revisions
![Screenshot 2023-12-24 at 9 44 43β―AM](https://private-user-images.githubusercontent.com/1456191/292654578-24837137-98f5-4ccc-aef0-11e12f696155.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQ2MDUzNjgsIm5iZiI6MTcyNDYwNTA2OCwicGF0aCI6Ii8xNDU2MTkxLzI5MjY1NDU3OC0yNDgzNzEzNy05OGY1LTRjY2MtYWVmMC0xMWUxMmY2OTYxNTUucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgyNSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MjVUMTY1NzQ4WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9ZDQyYTI5OTA1YjkzYzcxZDg3ZDdkMGZmZmU5YTk5OGQ3ODVkZjIyZmY5Y2FhZTIyYTJiNjQwZmFhYWQwMzA5NSZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.FKC9pVchphxODQGonMZoR5rake6OFoyyPwUziFSDOTQ)
Contents |
---|
About pairs & triplets |
Storing in pairs |
Sample of constructing a pair |
Sample of destructing a pair |
Using Triple |
- They are groups of two to three pieces of data.
- They can represent classes or pieces of data.
- This accepts two inputs it can be anything
primitive-type
,non-primitive-type
,objects
,collections
,etc
.. - So basically there are two parts
- Both parts can be of different type meaning ex: one can be
int
and another can bestring
etc..
private fun initiate() {
var fullName : Pair<String,String> = Pair("Tony","Stark")
println("My name is ${fullName.first} ${fullName.second}")
var studentDetails : Pair<Student,Address> = Pair(Student("Tony","21"),
Address("HSR layout","Bangalore"))
println("Super hero name is ${studentDetails.first.name} and from the place ${studentDetails.second.city}")
}
private fun initiate() {
var fullName : Pair<String,String> = Pair("Tony","Stark")
var (firstName,lastName) = fullName
println("My name is $firstName $lastName")
}
- Using tripe is similar to pair.
- Only difference is that it can hold three types in its three placeholders.
- Destructing the
triple
is the same as thepair
mentioned above.
private fun initiateSecond() {
var fullName : Triple<String,String,String> = Triple("Tony","Stark","Bangalore")
var (firstName,lastName,place) = fullName
println("My name is $firstName $lastName from the place $place")
}