less than 1 minute read


1. set

  • List와 비슷, 순서가 없고 중복을 허용하지 않음
  • hashSet, TreeSet, linkedHashSet 등이 있음

1.1 선언

1
2
3
4
5
//불변형
val set = setOf(1,2,3)

//가변형
val mutableSet = mutableSetOf("apple", "banana", "orange")


1.2 데이터 추가, 제거

1
2
3
4
5
6
7
8
9
val set = setOf(1,2,3)

set.add(4)

println(set.toString())   //[1,2,3,4]

set.remove(3)

println(set.toString())   //[1,3,4]



2. map

  • key, value 형태, key 값 검색해서 찾음
  • 키는 중복될 수 없지만 값은 중복될 수 있다


2.1 선언

1
2
val capitalCityMap: MutableMap<String,String> 
  = mutableMapOf("One" to 1, "Two" to 2, "Three" to 3)


2.2 데이터 추가, 제거

1
2
3
4
5
6
7
8
9
val map = mapOf("One" to 1, "Two" to 2)

map.put("Four" to 4)

println(map)    //[1,2,4]

map.remove("Two")

println(map)    //[1,4]