skip to content

Pair, Triple & Destructuring

Pair and Triple are lightweight tuples, created with constructors or the infix to, and destructurable via their componentN functions. Interviewers usually add the caveat: past two fields, a named data class communicates far more.

part ofKotlinoverview, primer and where to startread it →
on this pageshow

questions

5

How do you create a Pair in Kotlin, and what does the 'to' infix function do?

level: juniorimportance: must knowfreq 70%

answer

  1. 'to' = infix sugar for Pair(this, that)
  2. .first / .second / .third
  3. data class → equals/hashCode/copy free
  4. mapOf("a" to 1) — 'to' shines for maps
  5. Triple has no infix builder

basics

~10 s

A Pair holds two values. You make one with Pair(a, b) or, more commonly, with the 'to' word, like 1 to "one". 'to' just builds a Pair of its left and right values.

solid answer

~30 s

Pair<A, B> is a generic data class holding two values, accessed via .first and .second. You construct it with the Pair(a, b) constructor or the infix function 'to', defined as `infix fun <A, B> A.to(that: B): Pair<A, B>`, so `"key" to 42` produces Pair("key", 42). Because 'to' is just sugar for the constructor, it is most idiomatic when building maps: `mapOf("a" to 1, "b" to 2)`, where mapOf takes vararg Pair entries. Being a data class, Pair gets equals/hashCode/toString and copy for free. Triple<A, B, C> is the three-element analogue with .first/.second/.third.

code

kotlin · 5 lines
kotlin
val p = "key" to 42          // Pair<String, Int>
println(p.first)             // key
println(p.second)            // 42
val ages = mapOf("Ann" to 30, "Bob" to 25)
val t = Triple(1, "two", 3.0)

go deeper

for a junior

Can create a Pair both ways and knows .first/.second and the mapOf use case.

for a middle

Explains 'to' as an infix extension fun and notes Pair/Triple are data classes with generated members.

for a senior

Mentions precedence pitfalls and that Triple lacks an infix builder; reasons about when tuples vs named types are appropriate.

for a principal

Frames 'to' as readability sugar with no runtime magic and advises named data classes over tuples in public APIs for maintainability.

## What Pair and Triple are `Pair<A, B>` and `Triple<A, B, C>` are tiny **data classes** in the Kotlin standard library that bundle 2 or 3 values of possibly different types. They are Kotlin's lightweight tuples. - `Pair` exposes `first: A` and `second: B`. - `Triple` exposes `first: A`, `second: B`, `third: C`. Because they are `data class`es, the compiler generates `equals`, `hashCode`, `toString`, `copy`, and the `componentN` functions used for destructuring. ## Constructing a Pair There are two ways: ```kotlin val p1 = Pair("key", 42) // explicit constructor val p2 = "key" to 42 // infix 'to' function ``` The `to` function is defined in the standard library as: ```kotlin public infix fun <A, B> A.to(that: B): Pair<A, B> = Pair(this, that) ``` The `infix` keyword lets you call it without dots or parentheses (`a to b` instead of `a.to(b)`). It is generic over the receiver `A` and parameter `B`, so it works for any two types. ## Why 'to' exists — building maps The primary idiomatic use is constructing maps, because `mapOf` accepts `vararg pairs: Pair<K, V>`: ```kotlin val ages = mapOf("Ann" to 30, "Bob" to 25) ``` Each `"Ann" to 30` is a `Pair<String, Int>`; `mapOf` reads `.first` as the key and `.second` as the value. ## Triple ```kotlin val t = Triple(1, "two", 3.0) // Triple<Int, String, Double> ``` There is no infix builder for `Triple` — you always use the constructor. ## Gotcha: precedence `to` has low precedence, so `1 + 1 to 2` parses as `(1 + 1) to 2` = `Pair(2, 2)`, which is usually fine, but mixing `to` with other operators can surprise you. When in doubt, parenthesize.

  • Is 'to' available for Triple?
    No. There is no infix builder for Triple; you must call the Triple(a, b, c) constructor directly.
  • What does 'infix' actually do here?
    It allows calling the single-parameter member/extension function without a dot and parentheses, so `a.to(b)` can be written `a to b`.

'to' is like a stapler that clips two slips of paper (a key and a value) into one labelled bundle.

saying these in an interview costs you the question

  • Thinking 'to' is a language keyword rather than a stdlib extension function
  • Believing Pair stores values by name/key rather than positionally as first/second
  • Claiming there is a 'to' builder for Triple
  • Confusing 'to' the Pair builder with the range operator or downTo

context

open as a page

Explain how destructuring works for Pair and Triple, and what componentN functions are.

level: middleimportance: must knowfreq 65%

basics

~10 s

You can unpack a Pair into two variables in one line: val (k, v) = pair. Kotlin does this by calling component1() and component2() behind the scenes. Triple adds component3() for a third variable.

open as a page

What does toList() return on a Pair and a Triple, and what is its element type?

level: middleimportance: should knowfreq 45%

basics

~10 s

Calling toList() on a Pair gives a list of its two values; on a Triple, a list of its three values. The order matches first, second, (third).

open as a page

Pair and Triple are data classes — what does that give you for equality, hashing, and copying, and what are the implications for using them as map keys?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Because Pair and Triple are data classes, two pairs with equal contents are equal and hash the same, so they work well as map keys or set elements. You can also copy() one while changing a field.

open as a page

When are Pair and Triple the right tool, and when should you prefer a named data class instead? Why does Kotlin not offer larger tuples?

level: principalimportance: should knowfreq 35%

basics

~20 s

Use Pair or Triple for quick, local, throwaway groupings. For anything returned from a public function or stored long-term, use a named data class so the fields have meaningful names. Kotlin stops at three on purpose to discourage anonymous blobs.

open as a page