Skip to content
Related Articles

Related Articles

Scala ListBuffer
  • Last Updated : 07 Jun, 2019

A list is a collection which contains immutable data. List represents linked list in Scala. A List is immutable, if we need to create a list that is constantly changing, the preferred approach is to use a ListBuffer.
The Scala List class holds a sequenced, linear list of items. A List can be built up efficiently only from back to front. the ListBuffer object is convenient when we want to build a list from front to back. It supports efficient prepend and append operations.

Once we are done creating our list, call the toList method. To convert the ListBuffer into a List, Time taken will be constant. To use ListBuffer, scala.collection.mutable.ListBuffer class is imported, an instance of ListBuffer is created.

Example :

var name = new ListBuffer[datatype]()  // empty buffer is created

var name = new ListBuffer("class", "gfg", "geeksforgeeks")

In the above example, first, an empty buffer is created here datatype indicates the type of data such as integer, string. Then created a buffer with three elements, of type string.

Below operation can be performed on ListBuffer –



  • By using L += e we can appends the element e in constant time.
  • By using e +=: L we can prepends the element e in constant time.
  • L.toList In constant time, It returns a list with the contents of the ListBuffer . We should not use the ListBuffer once changing it to a list.

Operations on ListBuffer

Creating instance of ListBuffer:




// Scala program to create a ListBuffer 
// ListBuffer class is imported 
import scala.collection.mutable.ListBuffer
  
// Creating object 
object GfG 
  
    // Main Method 
    def main(args: Array[String]) 
    
        // Instance of ListBuffer is created 
        var name = ListBuffer[String]()  
        name += "GeeksForGeeks"
        name += "gfg"
        name += "Class"
        println(name) 
    


Output:

ListBuffer(GeeksForGeeks, gfg, Class)

 
Access element from ListBuffer:

Element is accessed same as list, ListBuffer(i) is used to accessed ith index element of list.




// Scala program to access element of ListBuffer 
// ListBuffer class is imported 
import scala.collection.mutable.ListBuffer 
  
// Creating Object 
object GFG 
      
    // Main Method 
    def main(args: Array[String]) 
    
        // Instance of ListBuffer is created 
        var name = ListBuffer[String]() 
        name += "GeeksForGeeks"
        name += "gfg"
        name += "Class"
          
        // Accessing 1th index element of listBuffer 
        println(name(1)) 
    


Output:

gfg

 
Adding elements in ListBuffer:

  • Add single element to the buffer
    ListBuffer+=( element)
  • Add two or more elements (method has a varargs parameter)
     ListBuffer+= (element1, element2, ..., elementN )
  • Append one or more elements (uses a varargs parameter)
    ListBuffer.append( elem1, elem2, ... elemN)




// Scala program to add element in ListBuffer 
// ListBuffer class is imported 
import scala.collection.mutable.ListBuffer 
  
// Creating Object 
object GFG 
      
    // Main Method 
    def main(args: Array[String]) 
    
        // Instance of ListBuffer is created 
        var name = ListBuffer[String]() 
      
        // Adding one element 
        name += "GeeksForGeeks"
              
        // Add two or more elements 
        name += ("gfg", "class"
          
        // Adding one or more element using append method 
        name.append("Scala", "Article"
              
        // Printing ListBuffer 
        println(name) 
    


Output:

ListBuffer(GeeksForGeeks, gfg, class, Scala, Article)

 
Deleting ListBuffer Elements:

  • Remove one element
    ListBuffer-= (element)
  • Remove multiple elements
    ListBuffer-= (elem1, elem2, ....., elemN)




// Scala program to delete element from ListBuffer 
// ListBuffer class is imported 
import scala.collection.mutable.ListBuffer 
  
// Creating Object 
object GFG 
      
    // Main Method 
    def main(args: Array[String]) 
    
        // Instance of ListBuffer is created 
        var name = ListBuffer( "GeeksForGeeks", "gfg"
                                "class", "Scala"
                                "Article"
      
        // Deletes one element 
        name -= "GeeksForGeeks"
              
        // Deletes two or more elements 
        name -= ("gfg", "class"
          
        // Printing resultant ListBuffer 
        println(name) 
    


Output:

ListBuffer(Scala, Article)

 
Deleting ListBuffer Elements using ListBuffer.remove() :

The remove() method is used to delete one element by its position in the ListBuffer, or a series of elements beginning at a starting position.




// Scala program for remove method, on ListBuffer 
// ListBuffer class is imported 
import scala.collection.mutable.ListBuffer 
  
// Creating Object 
object GFG 
      
    // Main Method 
    def main(args: Array[String]) 
    
        // Instance of ListBuffer is created 
        var name = ListBuffer( "GeeksForGeeks"
                                "gfg", "class"
                                "Scala", "Article"
          
        // Removing 0th index element 
        name.remove(0
          
        // Printing resultant ListBuffer 
        println(name) 
        name.remove(1, 3
          
        // Printing resultant ListBuffer 
        println(name) 
    


Output:

ListBuffer(gfg, class, Scala, Article)
ListBuffer(gfg)
My Personal Notes arrow_drop_up
Recommended Articles
Page :