Home » Scala language

How to delete elements from a list in Scala?

Scala | Deleting elements from a List: Here, we are going to learn how to delete/remove elements from a list in Scala?
Submitted by Shivang Yadav, on April 17, 2020

The list is an immutable data structure so deleting elements is note easily possible. One way to do this is to filter out elements using filtering methods like filter, partition, splitAt, take, etc.

Program:

object MyClass {
    def main(args: Array[String]) {
        var progLang = List("C++", "Java", "Scala", "Python")
        
        println("Programming Languages: " + progLang)
        
        var newLang = progLang.filter(_<"P")
        
        println("Programming Languages: " + newLang)
       
    }
}

Output

Programming Languages: List(C++, Java, Scala, Python)
Programming Languages: List(C++, Java)

Other methods to modify the list are using ListBuffer which is mutable that makes the deletion process easy.

We can delete an element from ListBuffer using,

  1. Using -= operator
  2. Using remove() method
  3. Using --= operator (deletes elements of another collection)

1) Using -= operator

The -= can delete single or multiple elements from the ListBuffer.

Syntax:

    ListBuffer -= element(s)

Program:

import scala.collection.mutable.ListBuffer

object MyClass {
    def main(args: Array[String]) {
        var progLang = ListBuffer("C", "C++", "Java", "Scala", "Python", "JavaScript")
        
        println("Programming Languages: " + progLang)
        
        println("Deleting single element")
        progLang -= "Java"
        println("Programming Languages: " + progLang)
        
        println("Deleting multiple elements")
        progLang -= ("C", "Python")
        println("Programming Languages: " + progLang)
    }
}

Output

Programming Languages: ListBuffer(C, C++, Java, Scala, Python, JavaScript)
Deleting single element
Programming Languages: ListBuffer(C, C++, Scala, Python, JavaScript)
Deleting multiple elements
Programming Languages: ListBuffer(C++, Scala, JavaScript)

2) Using remove() method

The remove() method is the same as -= that can delete single as well as multiple elements.

Syntax:

    ListBuffer.remove(element)

Program:

import scala.collection.mutable.ListBuffer

object MyClass {
    def main(args: Array[String]) {
        var progLang = ListBuffer("C", "C++", "Java", "Scala", "Python", "JavaScript")
        
        println("Programming Languages: " + progLang)
        
        println("Deleting single element")
        progLang.remove(0)
        println("Programming Languages: " + progLang)
        
        println("Deleting multiple elements")
        progLang.remove(3, 2)
        println("Programming Languages: " + progLang)
    }
}

Output

Programming Languages: ListBuffer(C, C++, Java, Scala, Python, JavaScript)
Deleting single element
Programming Languages: ListBuffer(C++, Java, Scala, Python, JavaScript)
Deleting multiple elements
Programming Languages: ListBuffer(C++, Java, Scala)

The remove() method works on indexes of the ListBuffer and while working with the deletion of multiple elements we need to start index and number to be deleted as parameters of the method.

3) Using --= operator

The --= deletes the elements that are specified using another collection.

Syntax:

    ListBuffer --= collection

Program:

import scala.collection.mutable.ListBuffer

object MyClass {
    def main(args: Array[String]) {
        var progLang = ListBuffer("C", "C++", "Java", "Scala", "Python", "JavaScript")
        
        println("Programming Languages: " + progLang)
        
        println("Deleting elements")
        progLang --= Array("C", "Python")
        println("Programming Languages " + progLang)
    }
}

Output

Programming Languages: ListBuffer(C, C++, Java, Scala, Python, JavaScript)
Deleting elements
Programming Languages ListBuffer(C++, Java, Scala, JavaScript)





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.