Scala
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Scala
-
Section 4: Functional Scala
-
Section 5: Collections in Depth
-
Section 6: Type System
-
Section 7: Concurrency and Ecosystem
-
Section 8: Practical Projects
-
Section 9: Interview Practice
-
Section 10: Data Structures and Algorithms in Scala
-
Section 11: More Practice Exercises
-
Section 12: Advanced Functional Patterns
-
Section 13: More Ecosystem
-
Section 14: Scala Collections Library Deep Dive
-
Section 15: Scala Standard Library Deep Dive
-
Section 16: Akka Ecosystem Deep Dive
-
Section 17: Cats and Cats Effect Deep Dive
-
Section 18: Play Framework Deep Dive
-
Section 19: Apache Spark with Scala Deep Dive
-
Section 20: Scala Build Tools Deep Dive
-
Section 21: Scala 3 Specific Features
-
90: Union and Intersection Types
-
Section 22: Scala Testing Deep Dive
-
Section 23: Functional Domain Modeling
-
Section 24: More Data Structures and Algorithms in Scala
-
Section 25: Scala for Data Engineering
-
Section 26: More Practical Projects
-
Section 27: More Interview and Review
-
Section 28: ZIO Ecosystem Deep Dive
-
Section 29: Scala for Machine Learning
-
Section 30: Scala Microservices Architecture
-
Section 31: Scala Type System Deep Dive
-
Section 32: More Practice and Drills
-
Section 33: Scala Performance Deep Dive
-
Section 34: Scala Ecosystem Tooling
-
Section 35: Scala for Reactive Systems
-
Section 36: More Real-World Case Studies
-
Section 37: Scala for Financial Systems
-
Section 38: Scala GraphQL and gRPC
-
Section 39: More Final Projects
-
Section 40: More Interview and Final Review
-
Section 41: Scala for Streaming Data
-
Section 42: Scala Security Practices
-
Section 43: More Language Deep Dive
-
Section 44: Scala Command-Line Tools
-
Section 45: Scala Documentation and Style
-
Section 46: Scala Dependency Management
-
Section 47: More Practical Backend Patterns
-
Section 48: Scala for Event-Driven Architecture
-
Section 49: More Practice Drills Round 2
-
Section 50: Scala Compiler Deep Dive
-
Section 51: Scala for Web Frontends
-
Section 52: More Data Engineering Practice
-
Section 53: Scala Observability
-
Section 54: More Advanced Practice Projects
-
Section 55: Scala for Legacy Java Integration
-
Section 56: More Testing Practice
-
Section 57: Final Mastery Review
-
Section 58: Scala History and Ecosystem Context
-
Section 59: More Concurrency Patterns
-
Section 60: Scala for Configuration Management
-
Section 61: More Domain Modeling Practice
23: List, Vector, and Array Operations
When should I actually use a List versus a Vector or an Array?
This is the most common question I get from people moving into Scala. On the surface, they all just "hold things," but their internal engines are completely different. If you pick the wrong one, your app will crawl once your data grows.
A List is a singly-linked list. It's fantastic if you're mostly adding elements to the front or processing things recursively. But if you try to access the 1,000th element of a List, Scala has to walk through the first 999 elements to get there. That's a performance killer.
A Vector is your "general purpose" workhorse. It's an immutable, indexed sequence. It provides nearly constant time for random access and appending to either end. If you aren't sure which one to use, just start with a Vector. I usually do.
Then there's the Array. This is a thin wrapper around a Java array. It's mutable and has a fixed size. I only use Arrays when I'm doing heavy numerical lifting or interfacing with a Java library that demands one. Since they're mutable, they can introduce bugs in multi-threaded code, so use them sparingly.
// A quick cheat sheet for your brain:
val myList = List("Apple", "Banana", "Cherry") // Great for prepending, bad for random access
val myVec = Vector("Apple", "Banana", "Cherry") // Great for everything, the safe default
val myArray = Array("Apple", "Banana", "Cherry") // Fast, mutable, fixed size
I keep seeing :: and +:—what's the difference in how they work?
In Scala, how you add an element matters. For a List, the :: (cons) operator is the gold standard. It adds an element to the front of the list. Because it's a linked list, this is an $O(1)$ operation—it's practically instant regardless of how big the list is.
Now, if you use +: to add something to the end of a List, Scala has to copy the entire list to attach that new element. If you do this inside a loop, you've just turned your program into a snail.
Vectors are different. They are designed to handle additions to both ends efficiently. Here is how that looks in practice:
val history = List("Page 1", "Page 2")
val updatedHistory = "Page 3" :: history
// Result: List("Page 3", "Page 2", "Page 1") - Fast!
val vecHistory = Vector("Page 1", "Page 2")
val updatedVec = vecHistory :+ "Page 3"
// Result: Vector("Page 1", "Page 2", "Page 3") - Also fast!
How do I actually transform this data without writing messy for-loops?
Coming from Java or C#, the instinct is to create an empty collection and loop through the data to fill it. Please, don't do that in Scala. We use higher-order functions like map, filter, and flatMap. They work almost identically across Lists, Vectors, and Arrays.
Let's say you have a collection of raw user input strings that are messy. You want to trim them, remove the empty ones, and capitalize them. I'd chain these operations together like this:
val rawInputs = Vector(" alice ", " ", "bob ", " charlie ")
val cleanedInputs = rawInputs
.map(_.trim) // Remove whitespace
.filter(_.nonEmpty) // Get rid of the empty strings
.map(_.capitalize) // Make them look pretty
// Result: Vector("Alice", "Bob", "Charlie")
I love this approach because it's declarative. You're telling Scala what you want, not how to move the pointers around. It's much harder to accidentally introduce an "off-by-one" error when you aren't managing indices manually.
What if I need to switch between these types?
You'll often find yourself in a situation where you've been using a Vector for the performance benefits, but a library function you're calling specifically requires a List or an Array. Luckily, Scala makes this trivial with conversion methods.
You just call .toList, .toVector, or .toArray. Just keep in mind that these methods create a new collection, so they aren't free. If you're doing this millions of times per second in a tight loop, it'll show up in your profiler.
val myVec = Vector(1, 2, 3)
val myList = myVec.toList // Now it's a List
val myArray = myVec.toArray // Now it's a Java-style Array
📋 Practical Task
Exercise: Order Processing Pipeline
You are building a small part of an e-commerce backend. You have a Vector of raw order amounts (as Double), but the data is noisy: it contains some negative numbers (errors) and some zero values that should be ignored.
Your Goal: Write a program that does the following:
- Starts with this input:
val rawOrders = Vector(120.50, -5.0, 45.0, 0.0, 300.25, -10.0, 15.75) - Filters out all values that are 0 or less.
- Applies a 10% tax to each remaining order using
map. - Converts the final result into an
Array(because the legacy payment gateway API requires anArray[Double]). - Prints the final
Arrayto the console.
Expected Output: An array containing [132.55, 49.5, 330.275, 17.325] (approximately).
There are no comments for now.