Updated July 1, 2023
Introduction to Scala ListBuffer
As we know, a List is used for strings and retrieving data, but scala lists are immutable in nature, and they are based upon the linked list data structure. ListBuffer is also a part of the collection only we should go for list buffer when the data inside the list is constantly changing, or we can say that the whole list is changing, then we can prefer listbuffer. It is also providing us with various methods, prepends, and appends operations as well. If we want to use this in the scala program, we have to import scala.collection.mutable.ListBuffer package into our class to use it and create its object.
Syntax:
We can create it in two ways:
1. Create an empty list buffer: In Scala, we can create an empty list buffer mean without initializing it. We need to specify the data type of the ListBuffer, and which type of data it is going to take.
varvariable_name = new ListBuffer[data_type]()
varlistbuff = new ListBuffer[Int]()
In the above syntax, we are defining the variable name and data type as Int for this ListBuffer.
2. Initialize while create: In Scala, we can create a listbuffer object, and at the same time, we can initialize it with our values of the same type.
varvariable_name = new ListBuffer("val1", "val2", "val3" ,"so on ...")
var name = new ListBuffer("abc", "xyz", "swer")
Just provide the variable name and assign values accordingly.
How ListBuffer work in Scala?
ListBuffer in Scala is a collection to store and retrieve elements, also, we can perform many operations on our data to modify it. 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.
ListBuffer is available inside scala.collection.mutable.ListBuffer package. It extends many classes and super types, which can be found below also its hierarchy. ListBuffer can be converted into List by calling toList() method.
We have one example to know if it’s working properly.
Extended Classes:
AbstractBuffer[A] | SeqOps[A, ListBuffer, ListBuffer[A]] | StrictOptimizedSeqOps[A, ListBuffer, ListBuffer[A]] | ReusableBuilder[A, immutable.List[A]] | IterableFactoryDefaults[A, ListBuffer |
Linear Super Types Classes:
DefaultSerializable | Java.io.Serializable | ResuableBuilder | immutableList | Builder |
ListBuffer | StrictOptimizedIteratorOps | ListBuffer[A] | AbstractBuffer | Buffer |
Shrikable | Growable | Clearable | AbstarctSeq | Collection.seq |
Equals | Collection.seqOps | PartialFuntion | Collection.AbstractIterable | Iterable |
IterableFactoryDefult | IterableOps | IterableOpsOps | IterableOnce | NA |
Code:
import scala.collection.mutable.ListBuffer
object Main extends App{
// Your code here!
// here we are creating the ListBuffer object
var list = ListBuffer[String]()
// assigning values
list += "Hello"
list += "byr"
list += "to all"
println(list)
}
So list buffer is used where data in the list is constantly changing because, as we know that, the list is immutable means once created object cannot change its value itself. So when our data is changing, we should go for listbuffer. After that, we can convert our listbuffer object to a list as well by using the toList method. Also, it is the implementation of List only in Scala. But if we want to search an element arbitrarily, we should go for ArrayBuffer we should not opt for ListBuffer in this case because the performance will be degraded. ArrayBuffer performs searching based on the index, so it is fast.
Examples of Scala ListBuffer
Given below are the examples mentioned:
Example #1
This will show how to create the instance of ListBuffer.
Code:
import scala.collection.mutable.ListBuffer
object Main extends App{
// Your code here!
// here we are creating the ListBuffer object
var list = ListBuffer[String]()
// assigning values
list += "Hello"
list += "byr"
list += "to all"
list += "to all 1"
list += "to all 2"
list += "to all 3"
println(list)
}
Output:
Example #2
In this example, we will see how to access the elements of ListBuffer.
Code:
import scala.collection.mutable.ListBuffer
object Main extends App{
// Your code here!
// here we are creating the ListBuffer object
var list = ListBuffer[String]()
// assigning values
list += "Hello"
list += "byr"
list += "to all"
list += "to all 1"
list += "to all 2"
list += "to all 3"
println("Accessing third element from listBuffer " + list(3))
}
Output:
Example #3
In this example, we will see append by using ListBuffer.append().
Code:
import scala.collection.mutable.ListBuffer
object Main extends App{
// Your code here!
// here we are creating the ListBuffer object
var list = ListBuffer[String]()
// assigning values
list += "Hello"
list += "byr"
list += "to all"
list += "to all 1"
list += "to all 2"
list += "to all 3"
println("values inside list before " + list)
// using method
list.append("new value append")
println("List after append method called :::: " + list)
}
Output:
Example #4
In this example, we will use the += operator to append values inside listBuffer.
Code:
import scala.collection.mutable.ListBuffer
object Main extends App{
// Your code here!
// here we are creating the ListBuffer object
var list = ListBuffer[String]()
// assigning values
list += "Hello"
list += "byr"
list += "to all"
list += "to all 1"
list += "to all 2"
list += "to all 3"
println("values inside list before " + list)
// using method
list += "new value append"
println("List after append operator called :::: " + list)
}
Output:
Example #5
In this example, we will see how to remove the element from ListBuffer. We are using -= for this to remove.
Code:
import scala.collection.mutable.ListBuffer
object Main extends App{
// Your code here!
// here we are creating the ListBuffer object
var list = ListBuffer[String]()
// assigning values
list += "Hello"
list += "byr"
list += "to all"
list += "to all 1"
list += "to all 2"
list += "to all 3"
println("values inside list before " + list)
// using method
list -= "to all"
println("List after removing operator called :::: " + list)
}
Output:
Conclusion
Scala ListBuffer is a good choice when our data for the particular list is changing very often, or we can say it is changing very frequently. As we all know, the List in Scala is immutable so that we can go for list in this case, but we can convert our ListBuffer object to List in scala. For this, they have tolist method.
Recommended Articles
We hope that this EDUCBA information on “Scala ListBuffer” was beneficial to you. You can view EDUCBA’s recommended articles for more information.