fun sampleCodeForMapOperator() {
val numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3)
println(numbersMap.get("one"))
println(numbersMap["one"])
println(numbersMap.getOrElse("four", { getSomethingInt() }))
println(numbersMap.getOrDefault("four", 10))
println(numbersMap["five"]) // null
//numbersMap.getValue("six") // exception!
}
fun getSomethingInt(): Int {
return 100
}
keys values 를 활용하여 map의 key / value 들을 한번에 살펴 볼 수 있다val numbersMap = mapOf("one" to 1, "two" to 2, "three" to 3)
println(numbersMap.keys)
println(numbersMap.values)
fun sampleCodeForFilterForMap() {
val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key11" to 11)
val filteredMap = numbersMap.filter { (key, value) -> key.endsWith("1") && value > 10}
println(filteredMap)
val numbersMapForOneProperty = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key11" to 11)
val filteredKeysMap = numbersMapForOneProperty.filterKeys { it.endsWith("1") }
val filteredValuesMap = numbersMapForOneProperty.filterValues { it < 10 }
println(filteredKeysMap)
println(filteredValuesMap)
}
+ / - operator를 활용 할 수 있다.+ operator를 활용하여 추가하려고 하는 Pair 객체의 key값이 이미 map에 존재 하는 경우, 기존의 Pair객체를 추가하려고 하는 Pair객체로 대체한다.- operator를 활용하는 경우, 기존의 map을 수정하는 것이 아닌 새로운 map 객체를 생성한다.- operator를 활용하는 경우, key값을 활용하여 Pair객체를 제거 할 수 있다.MutableMap 객체를 활용하여 map안의 데이터를 조작 할 수 있다.
데이터 조작 작업을 할 때, 정해진 규칙사항이 있다.
Add
put 을 활용하여 데이터 추가 가능put 함수는, map에 현재 추가하려는 Pair객체의 key값이 이미 존재하면 존재하는 key값의 value 값을 리턴하고, 그렇지 않으면 null 값을 리턴 ( putAll 함수는 값을 리턴하지 않음 ( Unit 리턴 ))fun sampleCodeForAddOperator() {
val numbersMap = mutableMapOf("one" to 1, "two" to 2)
numbersMap.put("three", 3)
println(numbersMap)
val numbersMapPutAll = mutableMapOf("one" to 1, "two" to 2, "three" to 3)
numbersMapPutAll.putAll(setOf("four" to 4, "five" to 5))
println(numbersMapPutAll)
val numbersMapChangeValue = mutableMapOf("one" to 1, "two" to 2)
val previousValue = numbersMapChangeValue.put("one", 11)
println("value associated with 'one', before: $previousValue, after: ${numbersMapChangeValue["one"]}")
println(numbersMapChangeValue)
val numbersMapShorthand = mutableMapOf("one" to 1, "two" to 2)
numbersMapShorthand["three"] = 3 // calls numbersMapShorthand.put("three", 3)
numbersMapShorthand += mapOf("four" to 4, "five" to 5)
println(numbersMapShorthand)
}
Remove
remove 를 활용하여 데이터 삭제 가능remove 를 활용하여, key값을 삭제하는 것도 remove와 동일한 역할remove 를 활용하여, value값을 삭제하면 첫번째로 검색되는 값이 삭제 됨fun sampleCodeForRemoveOperator() {
val numbersMap = mutableMapOf("one" to 1, "two" to 2, "three" to 3)
numbersMap.remove("one")
println(numbersMap)
numbersMap.remove("three", 4) //doesn't remove anything
println(numbersMap)
val numbersMapRemoveElement = mutableMapOf("one" to 1, "two" to 2, "three" to 3, "threeAgain" to 3)
numbersMapRemoveElement.keys.remove("one")
println(numbersMapRemoveElement)
numbersMapRemoveElement.values.remove(3)
println(numbersMapRemoveElement)
val numbersMapMinusOperator = mutableMapOf("one" to 1, "two" to 2, "three" to 3)
numbersMapMinusOperator -= "two"
println(numbersMapMinusOperator)
numbersMapMinusOperator -= "five" //doesn't remove anything
println(numbersMapMinusOperator)
}