ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Kotlin] Map ์‚ฌ์šฉ๋ฒ•
    Android/Kotlin 2021. 8. 12. 23:59
    ๋ฐ˜์‘ํ˜•
    • ์ด ๊ธ€์€ "์œค์žฌ์„ฑ์˜ Google ๊ณต์‹ ์–ธ์–ด Kotlin" ๊ฐ•์˜๋ฅผ ๋“ฃ๊ณ  ๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์œผ๋กœ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

    Map

    • Key์™€ Value์˜ ๊ฐ’์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ Collection ์ž…๋‹ˆ๋‹ค.

    • List์™€ ๋™์ผํ•˜๊ฒŒ ๊ฐ€๋ณ€ํ˜• Map์™€ ๋ถˆ๋ณ€ํ˜• Map์ด ์žˆ์Šต๋‹ˆ๋‹ค.


    immutable Map

    fun main(){
        val map1 = mapOf("key1" to 1, "key2" to 2, "key3" to 3)
        val map2 = mapOf<String, Char>("key1" to '1', "key2" to '2', "key3" to '3')
        println(map1)       // {key1=1, key2=2, key3=3}
    }
    • Kotlin์—์„œ๋Š mapOf() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด์„œ ์‰ฝ๊ฒŒ map์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • map์˜ ๊ฐ’์€ Key์™€ value๊ฐ€ ๊ตฌ๋ณ„๋˜์–ด์•ผ ํ•˜๊ณ  to๋ฅผ ํ†ตํ•ด์„œ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
    • mapOf์—์„œ ๊ฐ’์„ ์ž…๋ ฅํ•˜์ง€ ์•Š์œผ๋ฉด ํƒ€์ž… ์ถ”๋ก ์ด ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ๋Š” ํƒ€์ž…์„ ๊ผญ ๋ช…์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    mutable Map

    fun main(){
        val map1 = mutableMapOf<String,Int>("key1" to 1, "key2" to 2, "key3" to 3)
        val map2 = mutableMapOf<String, Any>("key1" to 1, "key2" to 2.2, "key3" to "3")
        println(map1)
    }
    • mutableMapOf() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ€๋ณ€ํ˜• Map์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • Map ๋˜ํ•œ Generic์œผ๋กœ ์ •์˜๋˜์–ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์–ด๋– ํ•œ ํƒ€์ž…๋„ ์„ค์ • ๊ฐ€๋Šฅํ•˜๋ฉฐ, ์—ฌ๋Ÿฌ ํƒ€์ž…์„ ๋ฐ›๊ธธ ์›ํ•  ๊ฒฝ์šฐ ๋ชจ๋“  ํƒ€์ž…์˜ ์ƒ์œ„ ํด๋ž˜์Šค์ธ Any ํด๋ž˜์Šค๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

    index ์ ‘๊ทผ

    fun main(){
        val map1 = mutableMapOf<String,Int>("key1" to 1, "key2" to 2, "key3" to 3)
        println(map1["key1"])       // 1
        println(map1.get("key2"))       // 2
    }
    • Kotlin์—์„œ ๋Œ€๊ด„ํ˜ธ ๋˜๋Š” get ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด์„œ value๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
    • ๊ฐ€๋ณ€ํ˜• Map์ผ ๊ฒฝ์šฐ ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด ๋˜ํ•œ ๋Œ€๊ด„ํ˜ธ์™€ setํ•จ์ˆ˜ ์•„๋‹ˆ๋ฉด putํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

    Method

    • size : Map์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
    • count() : Map์— ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ์˜ ๊ฐฏ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
      fun main(){
        val map1 = mutableMapOf<String,Int>("key1" to 1, "key2" to 2, "key3" to 3)
        println(map1.size)      // 3
        println(map1.count())        // 3
      }
      size์™€ count()๊ฐ€ ์‹ค์งˆ์ƒ ๊ฐ™์€ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ค์ง€๋งŒ ์ •ํ™•ํ•˜๊ฒŒ size๋Š” property์ด๊ณ  count()๋Š” method์ž…๋‹ˆ๋‹ค.

    • keys : Map์˜ key๋งŒ ๋‹ด๋Š” ์ปฌ๋ ‰์…˜์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
    • values : Map์˜ value๋งŒ ๋‹ด๋Š” ์ปฌ๋ ‰์…˜์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
      fun main(){
        val map1 = mutableMapOf<String,Int>("key1" to 1, "key2" to 2, "key3" to 3)
        println(map1.keys)
        println(map1.values)
      }
      keys์™€ values์˜ ํฐ ์ฐจ์ด์ ์€ key๋Š” ์„œ๋กœ ๊ฐ™์€ ํ‚ค๊ฐ€ ์กด์žฌํ•  ์ˆ˜ ์—†๊ณ , value๋Š” ๊ฐ™์€ ๊ฐ’์ด ์กด์žฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฆ‰, keys๋Š” ๊ฐ™์€ ํ‚ค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์€ ์ปฌ๋ ‰์…˜์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— MutableSet() ๊ฐ€ ๋ฐ˜ํ™˜์ด ๋˜๊ณ  values๋Š” MutableCollection์ด ๋ฐ˜ํ™˜์ด ๋ฉ๋‹ˆ๋‹ค.

    • containsKey : ํ•ด๋‹น ํ‚ค๊ฐ€ Map์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜
    • containsValue : ํ•ด๋‹น ๊ฐ’์ด Map์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜
      fun main(){
        val map1 = mutableMapOf<String,Int>("key1" to 1, "key2" to 2, "key3" to 3)
        println(map1.containsKey("key1"))       // true
        println(map1.containsKey("key4"))       // false
        println(map1.containsValue(1))      // true
        println(map1.containsValue(4))      // false
      }

    • remove(key) : ํ•ด๋‹น key๋ฅผ ๊ฐ€์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๋Š” ํ•จ์ˆ˜
      fun main(){
        val map1 = mutableMapOf<String,Int>("key1" to 1, "key2" to 2, "key3" to 3)
        map1.remove("key1")
        println(map1)       // {key2=2, key3=3}
      }

    Map ๋ณ€ํ™˜

    • toMap() : ๋ถˆ๋ณ€ํ˜• Map์„ ์ƒ์„ฑํ•ด ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜

    • toMutableMap() ๊ฐ€๋ณ€ํ˜„ Map์„ ์ƒ์„ฑํ•ด ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜

    • Example์€ ์ƒ๋žตํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

    ๋ฐ˜์‘ํ˜•

    'Android > Kotlin' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

    [Kotlin] Scope functions (let, with, run, apply, also) ์ดํ•ด  (0) 2021.11.08
    [Kotlin] Set ์‚ฌ์šฉ๋ฒ•  (0) 2021.08.13
    [Kotlin] List ์‚ฌ์šฉ๋ฒ•  (0) 2021.08.12
    [Kotlin] String ์‚ฌ์šฉ๋ฒ•  (0) 2021.08.12
    [Kotlin] Array ์‚ฌ์šฉ๋ฒ•  (0) 2021.08.12
Designed by Tistory.