Welcome to the next unit of this course!
Before we delve deeper into Scala essentials for interview preparation, we'll revisit some Scala features — specifically, Scala collections like Lists and Strings. These collections allow Scala to group multiple elements, such as numbers or characters, under a single entity.
Some of these concepts might already be familiar to you, so feel free to breeze through the beginning until we reach the main topics.
As a starting point, it's crucial to understand what Scala's collections are. They help us manage multiple values efficiently and are categorized into various types such as Lists
, Sets
, and Maps
.
We will focus mainly on Lists
and Strings
. An interesting point is that Lists
are immutable by default, meaning they are unchangeable after creation. However, Scala offers mutable collections if changes are required. Let's see some examples:
Scala1object Solution { 2 def main(args: Array[String]): Unit = { 3 // Defining a list and a string 4 val myList = List(1, 2, 3, 4) 5 val myString = "hello" 6 7 // Attempting to change the first element of both collections won't work for immutable lists 8 // Uncommenting the below line will result in an error 9 // myList(0) = 100 10 11 // Strings in Scala, like Java, are immutable 12 // myString(0) = 'H' // Would also result in an error 13 } 14}
Imagine having to take an inventory of all flora in a forest without a list — seems near impossible, right? That's precisely the purpose Lists
serve in Scala. They allow us to organize data so that each item holds a definite position or an index, facilitating access to or modification of individual items.
However, to modify Lists
in Scala, you may use ListBuffer
, which provides a mutable version:
Scala1import scala.collection.mutable.ListBuffer 2 3object Solution { 4 def main(args: Array[String]): Unit = { 5 // Creating a list 6 val fruits = ListBuffer("apple", "banana", "cherry") 7 8 // Add a new element at the end 9 fruits += "date" // ListBuffer("apple", "banana", "cherry", "date") 10 11 // Inserting an element at a specific position 12 fruits.insert(1, "bilberry") // ListBuffer("apple", "bilberry", "banana", "cherry", "date") 13 14 // Removing a particular element 15 fruits -= "banana" // ListBuffer("apple", "bilberry", "cherry", "date") 16 17 // Accessing elements using indexing 18 val firstFruit = fruits(0) // "apple" 19 val lastFruit = fruits.last // "date" 20 } 21}
Think of Strings
as sequences of letters or characters. So whether you're writing a message or noting a paragraph, they are strings in Scala, enclosed by double quotes.
Scala1object Solution { 2 def main(args: Array[String]): Unit = { 3 // Creating a string 4 val greetingString = "Hello, world!" 5 6 // Accessing characters using indexing 7 val firstChar = greetingString(0) // 'H' 8 val lastChar = greetingString.last // '!' 9 10 // Making the entire string lowercase 11 val lowercaseGreeting = greetingString.toLowerCase // "hello, world!" 12 } 13}
Though Strings
are immutable, you can use string methods such as toLowerCase
, toUpperCase
, trim
, etc., to effectively work with them, producing new strings.
Both Lists
and Strings
allow access to individual elements through indexing. In Scala, indexing starts from 0, implying that the first element is at index 0, the second at index 1, and so on.
We can perform many operations on our Lists
and Strings
. We can slice them, concatenate them, and even find occurrences of a particular element!
Scala1object Solution { 2 def main(args: Array[String]): Unit = { 3 // Define a list and a string 4 val myList = List(1, 2, 3, 4, 5) 5 val myString = "Hello" 6 7 // Slicing: myList.slice(start, end), where `start` is inclusive and `end` is exclusive 8 val sliceList = myList.slice(2, 4) // List(3, 4) 9 val sliceString = myString.slice(1, 3) // "el" 10 11 // Concatenation: myList ++ anotherList 12 val concatenateList = myList ++ List(6, 7, 8) // List(1, 2, 3, 4, 5, 6, 7, 8) 13 val concatenateString = myString + ", world!" // "Hello, world!" 14 15 // Repetition isn't directly supported, but we can use fill or mkString 16 val repeatList = List.fill(2)(myList).flatten // List(1, 2, 3, 4, 5, 1, 2, 3, 4, 5) 17 val repeatString = myString * 2 // "HelloHello" 18 19 // Finding the first occurrence of an element using indexOf 20 val index1InList = myList.indexOf(1) // 0 21 val index8InList = myList.indexOf(8) // -1 means not found 22 val indexInString = myString.toLowerCase.indexOf('l') // 2 23 24 // Sorting items in a list 25 val sortedList = myList.sorted(Ordering[Int].reverse) // List(5, 4, 3, 2, 1) 26 } 27}
And there you have it, a Scala toolkit of Lists
and Strings
!
Give yourself a pat on the back! You've navigated through Scala collections, primarily focusing on Lists
and Strings
, learning how to create, access, and manipulate them with various operations.
Up next, reinforce your understanding with plenty of hands-on practice. The comprehension of these concepts, combined with frequent practice, will enable you to tackle more complex problem statements with ease. Happy coding!