Scala Collections - ListBuffer



Scala provides a data structure, the ListBuffer, which is more efficient than List while adding/removing elements in a list. It provides methods to prepend, append elements to a list.

Declaring ListBuffer Variables

The following is the syntax for declaring an ListBuffer variable.

Syntax

var z = ListBuffer[String]()

Here, z is declared as an list-buffer of Strings which is initially empty. Values can be added by using commands like the following −

Command

z += "Zara";
z += "Nuha";
z += "Ayan";

Processing ListBuffer

Below is an example program of showing how to create, initialize and process ListBuffer −

Example

import scala.collection.mutable.ListBuffer 
object Demo {
   def main(args: Array[String]) = {
      var myList = ListBuffer("Zara","Nuha","Ayan")
      println(myList);
      // Add an element
      myList += "Welcome";
      // Add two element
      myList += ("To", "Tutorialspoint");
      println(myList);
      // Remove an element
      myList -= "Welcome";
      // print second element
      println(myList(1));
   }
}

Save the above program in Demo.scala. The following commands are used to compile and execute this program.

Command

\>scalac Demo.scala
\>scala Demo

Output

ListBuffer(Zara, Nuha, Ayan)
ListBuffer(Zara, Nuha, Ayan, Welcome, To, Tutorialspoint)
Nuha

Prepending Elements

You can prepend elements to the ListBuffer using the prepend method. This method adds elements to the beginning of the ListBuffer.

Example

Try following example for prepending elements to ListBuffer -

import scala.collection.mutable.ListBuffer

object Demo {
   def main(args: Array[String]) = {
      var myList = ListBuffer("Zara", "Nuha", "Ayan")
      // Prepend an element
      myList.prepend("Hello")
      println(myList)
      // Prepend multiple elements
      myList.prepend("Greetings", "From")
      println(myList)
   }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

ListBuffer(Hello, Zara, Nuha, Ayan)
ListBuffer(Greetings, From, Hello, Zara, Nuha, Ayan)

Clearing ListBuffer

You can clear all elements from a ListBuffer using the clear method. This method removes all elements, making the ListBuffer empty.

Example

Try following example for clearing a ListBuffer -

import scala.collection.mutable.ListBuffer

object Demo {
   def main(args: Array[String]) = {
      var myList = ListBuffer("Zara", "Nuha", "Ayan")
      println(myList)
      // Clear the ListBuffer
      myList.clear()
      println("ListBuffer after clear: " + myList)
   }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

ListBuffer(Zara, Nuha, Ayan)
ListBuffer after clear: ListBuffer()

Inserting Elements at Specific Index

You can insert elements at a specific index in the ListBuffer using the insert method. This allows you to add elements at any position within the ListBuffer.

Example

Try following example for inserting elements at given index to ListBuffer -

import scala.collection.mutable.ListBuffer

object Demo {
   def main(args: Array[String]) = {
      var myList = ListBuffer("Zara", "Nuha", "Ayan")
      println(myList)
      // Insert an element at index 1
      myList.insert(1, "Hello")
      println(myList)
      // Insert multiple elements at index 2
      myList.insertAll(2, Seq("Greetings", "From"))
      println(myList)
   }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

ListBuffer(Zara, Nuha, Ayan)
ListBuffer(Zara, Hello, Nuha, Ayan)
ListBuffer(Zara, Hello, Greetings, From, Nuha, Ayan)

Converting ListBuffer to List

You can convert a ListBuffer to an immutable List using the toList method. This is useful when you want to work with an immutable version of the ListBuffer.

Example

Try following example for converting a ListBuffer to list -

import scala.collection.mutable.ListBuffer

object Demo {
   def main(args: Array[String]) = {
      var myList = ListBuffer("Zara", "Nuha", "Ayan")
      println(myList)
      // Convert ListBuffer to List
      val immutableList = myList.toList
      println("Immutable List: " + immutableList)
   }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

ListBuffer(Zara, Nuha, Ayan)
Immutable List: List(Zara, Nuha, Ayan)

Sorting Elements in ListBuffer

You can sort the elements of a ListBuffer using the sorted method, which returns a new ListBuffer with the elements sorted.

Example

Try following example for sorting elements of ListBuffer -

import scala.collection.mutable.ListBuffer

object Demo {
   def main(args: Array[String]) = {
      var myList = ListBuffer("Zara", "Nuha", "Ayan")
      println(myList)
      // Sort the ListBuffer
      val sortedList = myList.sorted
      println("Sorted ListBuffer: " + sortedList)
   }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

ListBuffer(Zara, Nuha, Ayan)
Sorted ListBuffer: ListBuffer(Ayan, Nuha, Zara)

Reversing Elements in ListBuffer

You can reverse the elements of a ListBuffer using the reverse method, which returns a new ListBuffer with the elements in reverse order.

Example

Try following example for reversing elements in ListBuffer -

import scala.collection.mutable.ListBuffer

object Demo {
   def main(args: Array[String]) = {
      var myList = ListBuffer("Zara", "Nuha", "Ayan")
      println(myList)
      // Reverse the ListBuffer
      val reversedList = myList.reverse
      println("Reversed ListBuffer: " + reversedList)
   }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

ListBuffer(Zara, Nuha, Ayan)
Reversed ListBuffer: ListBuffer(Ayan, Nuha, Zara)

Appending Elements

You can append elements to the ListBuffer using the append method. This method adds elements to the end of the ListBuffer.

Example

Try following example for appending elements to ListBuffer -

import scala.collection.mutable.ListBuffer

object Demo {
   def main(args: Array[String]) = {
      var myList = ListBuffer("Zara", "Nuha", "Ayan")
      println(myList)
      // Append an element
      myList.append("Hello")
      println(myList)
      // Append multiple elements
      myList.append("Greetings", "From")
      println(myList)
   }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

ListBuffer(Zara, Nuha, Ayan)
ListBuffer(Zara, Nuha, Ayan, Hello)
ListBuffer(Zara, Nuha, Ayan, Hello, Greetings, From)

Removing Elements by Index

You can remove elements from the ListBuffer by specifying the index using the remove method. This method removes the element at the specified index.

Example

Try following example for removing element from a given index -

import scala.collection.mutable.ListBuffer

object Demo {
   def main(args: Array[String]) = {
      var myList = ListBuffer("Zara", "Nuha", "Ayan")
      println(myList)
      // Remove the element at index 1
      myList.remove(1)
      println(myList)
      // Remove the element at index 0
      myList.remove(0)
      println(myList)
   }
}

Save the above program in Demo.scala. Use the following commands to compile and execute this program.

Command

> scalac Demo.scala
> scala Demo

Output

ListBuffer(Zara, Nuha, Ayan)
ListBuffer(Zara, Ayan)
ListBuffer(Ayan)

Scala ListBuffer Summary

  • ListBuffer in Scala is a mutable data structure that provides efficient methods for adding and removing elements.
  • You can prepend, append, and remove elements from a ListBuffer using various methods.
  • ListBuffer can be cleared, elements can be inserted at specific indices, and it can be converted to an immutable List.
  • Advanced operations include sorting and reversing the elements of a ListBuffer.
  • You can also append elements and remove elements by specifying their index.
Advertisements