0Pricing
Scala for Backend Engineering & Functional Programming · 강의

List, Vector, Set, Map

핵심 컬렉션을 알아봅니다

List, Vector, Set, Map은(는) CoddyKit의 무료 Scala for Backend Engineering & Functional Programming 강의입니다. 이것은 4개 중 1번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Scala for Backend Engineering & Functional Programming 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Scala for Backend Engineering & Functional Programming 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Scala's core collections

Scala offers several immutable collections in its standard library. The four you will use most are List, Vector, Set, and Map. Each has different performance and semantics.

object Main {
  def main(args: Array[String]): Unit = {
    val list = List(1, 2, 3)
    val vector = Vector(1, 2, 3)
    val set = Set(1, 2, 3)
    val map = Map("a" -> 1, "b" -> 2)
    println(list)
    println(vector)
    println(set)
    println(map)
  }
}

List: a linked list

List is a singly-linked list. Prepending with :: is O(1) and fast, but random access and appending are O(n). It is ideal for recursion and stack-like use.

object Main {
  def main(args: Array[String]): Unit = {
    val xs = List(2, 3, 4)
    val prepended = 1 :: xs
    println(prepended)
    println("head: " + xs.head)
    println("tail: " + xs.tail)
  }
}

Vector: balanced and general

Vector is an indexed sequence with effectively O(1) access, update, prepend, and append. When you need fast random access or a general-purpose sequence, prefer Vector over List.

object Main {
  def main(args: Array[String]): Unit = {
    val v = Vector(10, 20, 30, 40)
    println(v(2))
    val updated = v.updated(0, 99)
    println(updated)
    println(v :+ 50)
  }
}

Set: unique elements

A Set stores distinct elements with no duplicates and no guaranteed order. Membership tests with contains are fast.

object Main {
  def main(args: Array[String]): Unit = {
    val s = Set(1, 2, 2, 3, 3, 3)
    println(s)
    println(s.contains(2))
    println(s + 4)
    println(s - 1)
  }
}

Set operations

Sets support mathematical operations: union (|), intersect (&), and diff (−−).

object Main {
  def main(args: Array[String]): Unit = {
    val a = Set(1, 2, 3)
    val b = Set(2, 3, 4)
    println(a union b)
    println(a intersect b)
    println(a diff b)
  }
}

Map: key-value pairs

A Map associates keys with values. Keys are unique. Create entries with the -> arrow, and look up values with get (returns Option) or apply.

object Main {
  def main(args: Array[String]): Unit = {
    val ages = Map("Ann" -> 30, "Bob" -> 25)
    println(ages("Ann"))
    println(ages.get("Cara"))
    println(ages.getOrElse("Cara", 0))
  }
}

Updating a Map immutably

Immutable maps return a new map when you add or remove entries; the original is unchanged. Use + to add or overwrite and - to remove.

object Main {
  def main(args: Array[String]): Unit = {
    val m = Map("a" -> 1)
    val m2 = m + ("b" -> 2)
    val m3 = m2 - "a"
    println(m)
    println(m2)
    println(m3)
  }
}

Iterating over a Map

Iterating a Map gives you key-value tuples. You can destructure them directly in a for-comprehension or with pattern matching.

object Main {
  def main(args: Array[String]): Unit = {
    val scores = Map("math" -> 90, "art" -> 75)
    for ((subject, score) <- scores) {
      println(s"$subject: $score")
    }
  }
}

Common methods shared by all

All these collections share a large common API: size, isEmpty, map, filter, foreach, and more. Learn the API once and it applies everywhere.

object Main {
  def main(args: Array[String]): Unit = {
    println(List(1, 2, 3).map(_ * 2))
    println(Vector(1, 2, 3).map(_ * 2))
    println(Set(1, 2, 3).map(_ * 2))
  }
}

Converting between collections

Conversion methods like toList, toVector, toSet, and toMap let you switch types easily. Converting to a Set removes duplicates.

object Main {
  def main(args: Array[String]): Unit = {
    val withDupes = List(1, 1, 2, 3, 3)
    println(withDupes.toSet)
    val pairs = List(("a", 1), ("b", 2))
    println(pairs.toMap)
  }
}

Choosing the right collection

Quick guide:

  • List — recursion, fast prepend, head/tail processing
  • Vector — general-purpose, fast indexed access
  • Set — uniqueness and membership tests
  • Map — key-based lookups
object Main {
  def main(args: Array[String]): Unit = {
    val ids = List(5, 3, 5, 1, 3)
    val unique = ids.toSet
    val indexed = ids.toVector
    println(s"unique count: ${unique.size}")
    println(s"third element: ${indexed(2)}")
  }
}

Quick Check

Which collection automatically removes duplicate elements?

Recap

You met Scala's core immutable collections:

  • List — linked list, fast prepend
  • Vector — indexed, well-balanced performance
  • Set — unique elements with set algebra
  • Map — key-value lookups with get/getOrElse

They share a rich common API and convert into one another easily.

자주 묻는 질문

“List, Vector, Set, Map” 강의는 무료인가요?

네 — “List, Vector, Set, Map” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Scala for Backend Engineering & Functional Programming 강의 전체를 잠금 해제할 수 있습니다. Scala for Backend Engineering & Functional Programming 강의에는 총 4개의 강의가 포함되어 있습니다.

“List, Vector, Set, Map”에서 뭘 배우나요?

핵심 컬렉션을 알아봅니다 브라우저에서 직접 실행하는 실습 코드로 Scala for Backend Engineering & Functional Programming을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Scala for Backend Engineering & Functional Programming을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Scala for Backend Engineering & Functional Programming은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 1번째 강의입니다.

“List, Vector, Set, Map” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Scala for Backend Engineering & Functional Programming 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Scala for Backend Engineering & Functional Programming 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. List, Vector, Set, Map
  2. 변환
  3. 폴딩과 리듀싱
  4. 그룹화와 정렬
← Scala for Backend Engineering & Functional Programming(으)로 돌아가기