Skip to Content

Map (Dictionary) trong Kotlin

1. Giới thiệu

Map lưu trữ các cặp key-value, với key là duy nhất.

2. Immutable Map

fun main() { val ages = mapOf( "Alice" to 25, "Bob" to 30, "Charlie" to 35 ) println(ages["Alice"]) // 25 println(ages.size) // 3 }

3. Mutable Map

fun main() { val ages = mutableMapOf("Alice" to 25) // Thêm ages["Bob"] = 30 ages.put("Charlie", 35) ages.putAll(mapOf("Dan" to 40, "Eve" to 28)) // Xóa ages.remove("Bob") println(ages) }

4. Truy cập giá trị

fun main() { val map = mapOf("a" to 1, "b" to 2, "c" to 3) println(map["a"]) // 1 println(map["z"]) // null println(map.getOrDefault("z", 0)) // 0 println(map.getOrElse("z") { -1 }) // -1 // Keys và values println(map.keys) // [a, b, c] println(map.values) // [1, 2, 3] }

5. Kiểm tra

fun main() { val map = mapOf("a" to 1, "b" to 2) println(map.containsKey("a")) // true println("a" in map) // true println(map.containsValue(2)) // true println(map.isEmpty()) // false }

6. Duyệt Map

fun main() { val map = mapOf("a" to 1, "b" to 2, "c" to 3) // Duyệt entries for ((key, value) in map) { println("$key -> $value") } // forEach map.forEach { (k, v) -> println("$k: $v") } // Duyệt keys for (key in map.keys) { println(key) } }

7. Filter và Map

fun main() { val ages = mapOf("Alice" to 25, "Bob" to 30, "Charlie" to 20) // Filter val adults = ages.filter { (_, age) -> age >= 25 } println(adults) // {Alice=25, Bob=30} // filterKeys / filterValues val short = ages.filterKeys { it.length <= 3 } val young = ages.filterValues { it < 25 } // Map values val doubled = ages.mapValues { (_, v) -> v * 2 } println(doubled) // {Alice=50, Bob=60, Charlie=40} // Map keys val upper = ages.mapKeys { (k, _) -> k.uppercase() } }

8. Merge và Plus

fun main() { val map1 = mapOf("a" to 1, "b" to 2) val map2 = mapOf("b" to 3, "c" to 4) // Plus - map2 ghi đè map1 val merged = map1 + map2 println(merged) // {a=1, b=3, c=4} // Minus val removed = map1 - "a" println(removed) // {b=2} }

9. Các loại Map

fun main() { // HashMap - không đảm bảo thứ tự val hashMap = hashMapOf("a" to 1) // LinkedHashMap - giữ thứ tự thêm vào val linkedMap = linkedMapOf("a" to 1, "b" to 2) // SortedMap - sắp xếp theo key val sortedMap = sortedMapOf("c" to 3, "a" to 1, "b" to 2) println(sortedMap) // {a=1, b=2, c=3} }

10. Ví dụ thực tế

fun main() { // Đếm tần suất val words = listOf("apple", "banana", "apple", "cherry", "banana", "apple") val frequency = mutableMapOf<String, Int>() for (word in words) { frequency[word] = frequency.getOrDefault(word, 0) + 1 } println(frequency) // {apple=3, banana=2, cherry=1} // Grouping data class Person(val name: String, val city: String) val people = listOf( Person("Alice", "NYC"), Person("Bob", "LA"), Person("Charlie", "NYC") ) val byCity = people.groupBy { it.city } println(byCity) }

📝 Tóm tắt

  • mapOf("key" to value) tạo immutable map
  • mutableMapOf() tạo mutable map
  • map[key] truy cập giá trị
  • filter, mapValues, mapKeys cho transformations
  • sortedMapOf() cho map có thứ tự
Last updated on