Home »
Scala
How to delete elements from a list in Scala?
By IncludeHelp Last updated : October 20, 2024
Scala List
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.
Example
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,
- Using -= operator
- Using remove() method
- Using --= operator (deletes elements of another collection)
Delete list elements using using -= operator
The -= can delete single or multiple elements from the ListBuffer.
Syntax
ListBuffer -= element(s)
Example
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)
Delete list elements using remove() method
The remove() method is the same as -= that can delete single as well as multiple elements.
Syntax
ListBuffer.remove(element)
Example
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.
Delete list elements using --= operator
The --= deletes the elements that are specified using another collection.
Syntax
ListBuffer --= collection
Example
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)