[Kotlin] - Pair

Pair๋ž€

Pair๋ž€, ํ•œ ์Œ;๋‘ ๋ถ€๋ถ„์ด ํ•จ๊ป˜ ๋ถ™์–ด ํ•˜๋‚˜๋ฅผ ์ด๋ฃจ๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค.

kotlin์—์„œ๋„ ๋™์ผํ•˜๊ฒŒ ์Œ์„ ์˜๋ฏธํ•˜๋Š” ํด๋ž˜์Šค์ด๋ฉฐ, ํด๋ž˜์Šค ๊ตฌ์กฐ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ƒ๊ฒผ๋‹ค.

public data class Pair<out A, out B>(
    public val first: A,
    public val second: B
) : Serializable

์ž์„ธํ•œ ๋‚ด์šฉ์€ ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.

Pair์˜ ์‚ฌ์šฉ

์•ž์„œ ์„ค๋ช…ํ•œ ๊ฒƒ๊ณผ ๊ฐ™์ด 2๊ฐœ์˜ ๋ณ€์ˆ˜๋ฅผ ๋ฌถ์–ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์–ด๋– ํ•œ ํƒ€์ž…์ด๋“  ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.

๋ฐ์ดํ„ฐ ์ €์žฅ

์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์ •์˜๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

var a = Pair(1, "์งฑ๊ตฌ")
val b = Pair<Int, String>(second = "์ฒ ์ˆ˜", first = 2)
val (c1, c2) = Pair(3, "์œ ๋ฆฌ")
val (d1, d2) = 4 to "ํ›ˆ๋ฐœ๋กฌ"

์•ฝ๊ฐ„ ์ƒ์†Œํ•œ ์ฝ”๋“œ๊ฐ€ ์žˆ๋‹ค๋ฉด c1, c2 ๋ณ€์ˆ˜๋ฅผ ๊ด„ํ˜ธ๋กœ ๋ฌถ์–ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. Pair๊ฐ€ ํ•œ ์Œ์„ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ํ•œ ์Œ์˜ ๋ณ€์ˆ˜๋กœ ๋ฌถ์–ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

๋ฐ์ดํ„ฐ ์ถœ๋ ฅ

์ถœ๋ ฅ์€ Pair์˜ ์ฒซ ๋ถ€๋ถ„์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด first๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ๋ถ€๋ถ„์„ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด second๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋œ๋‹ค.

println("a.first = ${a.first} \t a.second = ${a.second}")

println("a = $a")

println("c.first = $c1 \t c.second = $c2")

// a.first = 1 	 a.second = ์งฑ๊ตฌ
println("a.first = ${a.first} \t a.second = ${a.second}")
// a = (1, ์งฑ๊ตฌ)
println("a = $a")

// b.first = 2 	 b.second = ์ฒ ์ˆ˜
println("b.first = ${b.first} \t b.second = ${b.second}")
// b = (2, ์ฒ ์ˆ˜)
println("b = $b")

// c.first = 3   	 c.second = ์œ ๋ฆฌ
println("c.first = $c1 \t c.second = $c2")

// d.first = 4 	     d.second = ํ›ˆ๋ฐœ๋กฌ
println("d.first = $d1 \t d.second = $d2")

๋ฐ์ดํ„ฐ ์ˆ˜์ •

์•ž์„œ ํด๋ž˜์Šค ๊ตฌ์กฐ๋ฅผ ๋ณด๋ฉด ์•Œ๋“ฏ์ด val๋กœ ๋˜์–ด ์žˆ์–ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋Ÿผ์—๋„ ๊ผญ ์ˆ˜์ •์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ copy๋ฅผ ํ†ตํ•ด ๋‹ค์‹œ ํ• ๋‹นํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๋‹ค.

a = a.copy(first = 5, second = "๋Œ")

๋ฐ์ดํ„ฐ ์ •๋ ฌ

Pair๋Š” ๋‹ค๋ฅธ ํด๋ž˜์Šค์™€ ๋‹ค๋ฅด๊ฒŒ PriorityQueue๋ฅผ ํ†ตํ•ด ์ •๋ ฌํ•  ์ˆ˜ ์—†๋‹ค.

fun main() {
    val pq = PriorityQueue<Pair<Int, Int>>()
    pq.offer(1 to 1)
    pq.offer(2 to 2)

    while (!pq.isEmpty()) {
        val cur = pq.poll()

        println(cur.first)
    }
}

์œ„์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜๋ฉด ์ปดํŒŒ์ผ ํƒ€์ž„์—๋Š” ์—๋Ÿฌ๋‚˜ ๋‚˜์ง€ ์•Š์ง€๋งŒ, ๋Ÿฐํƒ€์ž„์—์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

Exception in thread "main" java.lang.ClassCastException: 
class kotlin.Pair cannot be cast to class java.lang.Comparable 
(kotlin.Pair is in unnamed module of loader 'app'; java.lang.Comparable is in module java.base of loader 'bootstrap')

์—๋Ÿฌ ๋‚ด์šฉ์„ ์‚ดํŽด๋ณด๋ฉด Pair ํด๋ž˜์Šค๋Š” Comparable๋กœ ์บ์ŠคํŠธํ•  ์ˆ˜ ์—†๋‹ค๋Š” ๋‚ด์šฉ์ด๋‹ค. ์ฆ‰, Comparable์„ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•„ ๋ฌด์—‡๊ณผ ๋น„๊ต๋ฅผ ํ•ด์„œ ์ •๋ ฌํ• ์ง€ ์ •์˜๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์ƒ๊ฐ๋ณด๋‹ค ๊ฐ„๋‹จํ•˜๋‹ค.

// ๋ฐฉ์‹ 1
val pq = PriorityQueue<Pair<Int, Int>>(Comparator() { a, b -> a.first - b.first })

// ๋ฐฉ์‹ 2
val pq = PriorityQueue<Pair<Int, Int>> { a, b -> a.first - b.first }

์œ„์™€ ๊ฐ™์ด Comparator๋ฅผ ์ œ๊ณตํ•ด ์ •๋ ฌ์„ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ