Updated March 29, 2023
Introduction to Scala Mutable List
The list is an implementation of the collection in scala, and they are immutable. The list uses the linked list data structure. A mutable list is also part of the collection in scala and used to store and retrieve data. Scala provides us with many classes to create a mutable list. Mutable means the object inside the list is constantly changing. If we have this requirement whereof a list of objects is constantly changing, then we should go for a mutable list. The mutable class also provides many operations to perform on the list. There is also a method to convert the mutable list to again immutable list.
Syntax
var variabe_name = new Mutable_class_name[datatype]()
var variabe_name = new Mutable_class_name(val1, val2, val3, val4, val5 , val so on ...)
In the above syntax, we can create a mutable list in two ways;
1) The first one is to create an empty mutable list with its data type mentioned.
2) The second way is to create and initialize the list at the same time.
How does Mutable List work in Scala?
Scala provides us with various classes to create a mutable list. The element inside the list is constantly changing, then we can go for a mutable list, but this mutable list we can again convert to an immutable list by using the toList method. Let’s discuss the various mutable classes one by one;
1. Mutable List
This class internally uses the queue. Queue follows the FIFO approach that means first in, first out. The element which is inserting first will be removed out first. In the queue, we always add elements at the end of the queue and delete the element from the top of the queue.
Extended class:
- AbstractSeq[A]
- LinearSeq[A]
- LinearSeqOptimized[A, MutableList[A]]
- GenericTraversableTemplate[A, MutableList]
- Builder[A, MutableList[A]]
- Serializable
Supertypes of Mutable list;
- (Int) ⇒ A, , collection.Iterable[A]
- Seq[A]
- Mutable
- Iterable[A], Traversable[A]
- Cloneable[Seq[A]]
- AbstractTraversable[A]
- TraversableOnce[A]
- SeqLike[A, Seq[A]]
- io.Serializable
- Serializable
- Builder[A, MutableList[A]]
- Growable[A]
- Clearable
- Traversable[A]
- GenTraversable[A]
- Parallelizable[A, ParSeq[A]]
- GenTraversableOnce[A]
- FilterMonadic[A, MutableList[A]]
- HasNewBuilder[A, MutableList[A]]
- LinearSeqOptimized[A, MutableList[A]]
- LinearSeq[A]
- LinearSeqLike[A, MutableList[A]]
- SeqLike[A, MutableList[A]]
- Cloneable
- lang.Cloneable
- AbstractSeq[A]
- GenSeq[A]
- GenSeqLike[A, MutableList[A]]
- PartialFunction[Int, A]
- AbstractIterable[A]
- IterableLike[A, MutableList[A]]
- Equals
- GenIterable[A]
- TraversableLike[A, MutableList[A]]
- GenTraversableLike[A, MutableList[A]]
- AnyRef
- Any
- GenericTraversableTemplate[A, MutableList]
- LinearSeq[A]
- AbstractSeq[A]
- Seq[A]
- GenIterableLike[A, MutableList[A]]
Known sun classes for mutable list;
- Queue
- QueueProxy
- SynchronizedQueue
2. ListBuffer
ListBuffer is another kind of mutable class available in Scala. ListBuffer is available inside this package >> scala.collection.mutable.ListBuffer. Scala ListBuffer gives us constant time appends and prepends operation. But most of the other operations are linear. ListBuffer is the implementation of List only.
Extended class :
- AbstractBuffer[A]
- SeqOps[A, ListBuffer, ListBuffer[A]]
- StrictOptimizedSeqOps[A, ListBuffer, ListBuffer[A]]
- ReusableBuilder[A, immutable.List[A]]
- IterableFactoryDefaults[A, ListBuffer]
- DefaultSerializable
3. LinkedList
This is another form of a mutable list in the scala. In this, we create the head of the list, and this list can be manipulated and created manually. This is available inside scala.collection.mutable.LinkedList package.
Extended class :
- AbstractSeq[A]
- LinearSeq[A]
- GenericTraversableTemplate[A, LinkedList]
- LinkedListLike[A, LinkedList[A]]
- Serializable
4. DoubleLinkedList
This class internally uses a linked list that contains the head and tail of the node. Node has two parts head and tail. Also, the address of the next and previous node. It is available inside scala.collection.mutable.DoubleLinkedList class.
Extended class:
- AbstractSeq[A]
- LinearSeq[A]
- GenericTraversableTemplate[A, DoubleLinkedList]
- Serializable
- DoubleLinkedListLike[A, DoubleLinkedList[A]]
Examples of Scala Mutable List
Different examples are mentioned below:
Example #1
In this, we are creating a mutable list using a list buffer and printing it’s all elements.
Code:
import scala.collection.mutable.ListBuffer
object Main extends App{
// Your code here!
val list1 = new ListBuffer[Int]
list1 += 10
list1 += 20
list1 += 30
println("values inside list is ::")
println(list1)
}
Output:
Example #2
In this example, we are deleting elements from the mutable list using the -= method available in scala.
Code:
import scala.collection.mutable.ListBuffer
object Main extends App{
// Your code here!
val list1 = new ListBuffer[Int]
list1 += 10
list1 += 20
list1 += 30
println("values inside list before ::")
println(list1)
list1 -= 30
println("values inside list after ::")
println(list1)
}
Output:
Example #3
In this example, we calculate the elements present inside the list by using the size method.
Code:
import scala.collection.mutable.ListBuffer
object Main extends App{
// Your code here!
val list1 = new ListBuffer[Int]
list1 += 10
list1 += 20
list1 += 30
println("values inside list before ::")
println(list1)
list1 -= 30
var size = list1.size
println("values inside list after ::")
println(list1)
println("size of the list is ::")
println(size)
}
Output:
Example #4
In this example, we are obtaining the index of the element inside the mutable list by using the indexOf method available in the scala collection.
Code:
import scala.collection.mutable.ListBuffer
object Main extends App{
// Your code here!
val list1 = new ListBuffer[Int]
list1 += 10
list1 += 20
list1 += 30
println("values inside list before ::")
println(list1)
list1 -= 30
var index = list1.indexOf(10)
println("values inside list after ::")
println(list1)
println("index of the list is ::")
println(index)
}
Output:
Example #5
In this example, we are getting the head of the list by using the head method available in the scala collection.
Code:
import scala.collection.mutable.ListBuffer
object Main extends App{
// Your code here!
val list1 = new ListBuffer[Int]
list1 += 10
list1 += 20
list1 += 30
list1 += 50
list1 += 40
list1 += 60
println("values inside list before ::")
println(list1)
list1 -= 30
var head = list1.head
println("values inside list after ::")
println(list1)
println("head of the list is ::")
println(head)
}
Output:
Example #6
In this example, we are retrieving the last element present in the mutable list. For this, we are using the last method, which is available and the part of a collection only.
Code:
import scala.collection.mutable.ListBuffer
object Main extends App{
// Your code here!
//creating mutable list
val list1 = new ListBuffer[Int]
list1 += 10
list1 += 20
list1 += 30
println("values inside list before ::")
println(list1)
list1 -= 30
var last = list1.last
//printing output after operations
println("values inside list after ::")
println(list1)
println("last of the list is ::")
println(last)
}
Output:
Conclusion
Scala provides us with various classes to create the mutable list. They are part of the collection only. But they use different data structures to implement a mutable concept. We have list =buffer, linked List, Doubly Linked List, and so many. Also, they are come up with various methods to create and manipulate the list element. We can also convert out mutable list to an immutable list by using the toList Method.
Recommended Articles
This is a guide to Scala Mutable List. Here we discuss How Mutable List works in Scala and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –