Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- webpack
- 답글
- 상속
- sub query
- 버전일치
- java#왕초보
- 왕초보
- 숫자
- 오버라이드
- 시큐어코딩
- 함수
- React
- 스프링
- 쿠키
- 스타일보험
- 코틀린
- Spring
- git
- degit
- snowpack
- kotlin
- 안드로이드
- java
- SQL
- parcel
- FIle
- Spinner
- 미니게임
- Android
- 게시판
Archives
- Today
- Total
YSHUSH
Map 본문
Map 은 한쌍으로 관리가 되는데 key값과 value값을 사용한다.
기본 Map 사용법
- 수정, 삭제, 추가 불가
fun main(args: Array<String>) {
val langMap:Map<Int, String> = mapOf(1001 to "kotlin", 1002 to "Java", 1003 to "react")
for((key, value) in langMap){
println("key=$key, value=$value")
}
println(langMap[1002])
println(langMap.get(1001))
println(langMap)
println(langMap.keys)
}
가변형 Map인 mutableMap의 사용법 + 정렬방법
- 수정, 삭제, 추가 가능
// 정렬
// var sortedByValue = capitalCityMap.toList().sortedBy { it.first } // 올림
var sortedByValue = capitalCityMap.toList().sortedByDescending { it.first } // 내림
println(sortedByValue)
}
fun main(args: Array<String>) {
// 가변형 map
val capitalCityMap:MutableMap<String, String> = mutableMapOf("Korea" to "Seoul", "China" to "Beijing", "Japan" to "Tokyo")
println(capitalCityMap)
println(capitalCityMap.keys)
println(capitalCityMap.values)
capitalCityMap.put("UK", "London")
capitalCityMap.remove("China")
println(capitalCityMap)
val addData = mutableMapOf("USA" to "Washington", "India" to "NewDelhi")
capitalCityMap.putAll(addData)
println(capitalCityMap)
// 정렬
// var sortedByValue = capitalCityMap.toList().sortedBy { it.first } // 올림
var sortedByValue = capitalCityMap.toList().sortedByDescending { it.first } // 내림
println(sortedByValue)
}
'Coding > Kotlin' 카테고리의 다른 글
야구선수 관리 프로그램 (0) | 2022.01.31 |
---|---|
File (0) | 2022.01.27 |
List (0) | 2022.01.27 |
Generic (0) | 2022.01.27 |
Composition (0) | 2022.01.27 |