일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- Mac
- Java
- django
- numpy
- math
- Google Spreadsheet
- PostgreSQL
- Google Excel
- 파이썬
- Excel
- PANDAS
- gas
- Kotlin
- array
- SQL
- Tkinter
- c#
- google apps script
- dataframe
- PySpark
- matplotlib
- string
- Python
- Apache
- GIT
- list
- Redshift
- hive
- Github
- Today
- Total
달나라 노트
Kotlin - Set 본문
Original source = play.kotlinlang.org/byExample/01_introduction/01_Hello%20world
Kotlin에도 Set이라는 자료형이 있습니다.
Python과 동일하게 순서가 없으며 중복값이 허용되지 않는 collection이죠.
Kotlin에서는 두 가지 종류의 set이 존재합니다.
Mutable Set= 변경(요소 추가 제거 등)이 가능한 set
Set = 변경이 불가하고 읽기만 가능한 set
List를 생성할 때는 내장 method를 사용해야하는데 어떤 Method를 사용하는지에 따라 Mutable set인지 read-only set인지가 결정됩니다.
mutableSetOf() -> mutable set 생성
setOf() -> read-only set 생성
val set_colors: MutableSet<String> = mutableSetOf("Blue", "Black", "White") // 1
val set_colors_2: Set<String> = set_colors // 2
val set_numbers: Set<Int> = setOf(1, 2, 3) // 3
fun getlistnumbers(): Set<Int> { // 4
return set_numbers
}
fun getlistcolors(): MutableSet<String> { // 4
return set_colors
}
fun main() {
println(set_colors)
println(set_colors_2)
println(set_numbers)
set_colors.add("Sky") // 5
println(set_colors)
set_colors.add("Blue") // 6
println(set_colors)
// list_colors_2.add("Red") // Error 발생 // 7
// list_numbers.add(4) // Error 발생 // 7
println(getlistnumbers())
println(getlistcolors())
}
-- Result
[Blue, Black, White]
[Blue, Black, White]
[1, 2, 3]
[Blue, Black, White, Sky]
[Blue, Black, White, Sky]
[1, 2, 3]
[Blue, Black, White, Sky]
1. Mutable Set을 생성하고 있습니다. 자료형이 MutableSet<String>인 걸로 보아 String type 데이터들이 set의 요소로 들어가겠네요. 또한 mutableSetOf method를 사용하고 있습니다.
2. Mutable Set인 set_colors를 set_colors_2에 할당하고있는데 set_colors_2는 read-only set입니다. 따라서 set_colors_2는 read-only set이 될겁니다.
3. type이 Set<Int>인것으로 보아 read-only set이며 Int type 데이터가 set의 요소로 들어올 것을 알 수 있습니다.
4. 마찬가지로 function의 return 값이 Set이라면 MutableSet<요소 data type> 또는 Set<요소 data type> 등의 return type을 적어줘야 합니다.
5. set_colors에 요소를 추가하였습니다. Sky라는 요소가 추가된 것을 볼 수 있습니다.
6. set_colors에 Blue를 추가하고있지만 이미 set_colors에는 Blue라는 값이 존재합니다. set은 중복값을 허용하지 않으므로 Blue는 2개가 되지 않고 1개로 유지될겁니다.
7. read-only set에는 요소 추가가 불가합니다.
Set에 대한 추가 예시입니다.
위 내용을 토대로 아래 코드를 이해해봅시다.
fun main() {
var set_mutable: MutableSet<Any> = mutableSetOf(1, 2, 3, 3, 4, 4, "Apple", "Apple", "Orange")
var set_immutable: Set<Int> = setOf(1, 2, 3, 3, 4, 4, 5, 6)
fun show_mutable(x: MutableSet<Any>) {
for (i in x) {
println(i)
}
}
fun show_immutable(x: Set<Int>) {
for (i in x) {
println(i)
}
}
println(set_mutable)
println(set_immutable)
show_mutable(set_mutable)
show_immutable(set_immutable)
}
-- Result
[1, 2, 3, 4, Apple, Orange]
[1, 2, 3, 4, 5, 6]
1
2
3
4
Apple
Orange
1
2
3
4
5
6
'Kotlin' 카테고리의 다른 글
Kotlin - filter function (0) | 2021.03.15 |
---|---|
Kotlin - Map collection (0) | 2021.03.15 |
Kotlin - List (0) | 2021.03.15 |
Kotlin - Loops (0) | 2021.03.15 |
Kotlin - when 구문 (0) | 2021.03.15 |