A set is a collection which only contains unique items which are not repeatable and a list is a collection which contains immutable data. In scala, ListSet class implements immutable sets using a list-based data structure. Elements are stored in reversed insertion order, That means the newest element is at the head of the list. It maintains insertion order. Listset is used only for a small number of elements. We can create empty ListSet either by calling the constructor or by applying the function ListSet.empty. It’s iterate and traversal methods visit elements in the same order in which they were first inserted.
Syntax:
var ListSetName = ListSet(element1, element2, element3, ....)
Operations with ListSet
Initialize a ListSet : Below is the example to create or initialize ListSet.
Scala
import scala.collection.immutable. _
object GFG
{
def main(args : Array[String])
{
println("Initializing an immutable ListSet ")
val listSet 1 : ListSet[String] = ListSet("GeeksForGeeks",
"Article", "Scala")
println(s"Elements of listSet 1 = $listSet 1 ")
}
}
|
Output:
Initializing an immutable ListSet
Elements of listSet1 = ListSet(Scala, Article, GeeksForGeeks)
Check specific elements in ListSet :
Scala
import scala.collection.immutable. _
object GFG
{
def main(args : Array[String])
{
println("Initializing an immutable ListSet ")
val listSet 1 : ListSet[String] = ListSet("GeeksForGeeks",
"Article", "Scala")
println(s"Elements of listSet 1 = $listSet 1 ")
println("Check elements of immutable ListSet")
println(s"GeeksForGeeks = ${listSet 1 ("GeeksForGeeks")}")
println(s"Student = ${listSet 1 ("Student")}")
println(s"Scala = ${listSet 1 ("Scala")}")
}
}
|
Output:
Initializing an immutable ListSet
Elements of listSet1 = ListSet(Scala, Article, GeeksForGeeks)
Check elements of immutable ListSet
GeeksForGeeks = true
Student = false
Scala = true
Adding an elements in ListSet : We can add an element in ListSet by using + operator. below is the example of adding an element in ListSet.
Scala
import scala.collection.immutable. _
object GFG
{
def main(args : Array[String])
{
println("Initializing an immutable ListSet ")
val listSet 1 : ListSet[String] = ListSet("GeeksForGeeks",
"Article", "Scala")
println(s"Elements of listSet 1 = $listSet 1 ")
println("Add element of immutable ListSet ")
val listSet 2 : ListSet[String] = listSet 1 + "Java"
println(s"Adding element java to ListSet $listSet 2 ")
}
}
|
Output:
Initializing an immutable ListSet
Elements of listSet1 = ListSet(Scala, Article, GeeksForGeeks)
Add element of immutable ListSet
Adding element java to ListSet ListSet(Java, Scala, Article, GeeksForGeeks)
Adding two ListSet : We can add two ListSet by using ++ operator. below is the example of adding two ListSet.
Scala
import scala.collection.immutable. _
object GFG
{
def main(args : Array[String])
{
println("Initializing an immutable ListSet ")
val listSet 1 : ListSet[String] = ListSet("GeeksForGeeks",
"Article", "Scala")
println(s"Elements of listSet 1 = $listSet 1 ")
val listSet 2 : ListSet[String] = listSet 1 ++ ListSet("Java",
"Csharp")
println(s"After adding two lists $listSet 2 ")
}
}
|
Output:
Initializing an immutable ListSet
Elements of listSet1 = ListSet(Scala, Article, GeeksForGeeks)
After adding two lists ListSet(Java, Csharp, Scala, Article, GeeksForGeeks)
Remove element from the ListSet : We can remove an element in ListSet by using – operator. below is the example of removing an element in ListSet.
Scala
import scala.collection.immutable. _
object GFG
{
def main(args : Array[String])
{
println("Initializing an immutable ListSet ")
val listSet 1 : ListSet[String] = ListSet("GeeksForGeeks",
"Article", "Scala")
println(s"Elements of listSet 1 = $listSet 1 ")
println("Remove element from the ListSet ")
val listSet 2 : ListSet[String] = listSet 1 - ("Article")
println(s"After removing element from listset = $listSet 2 ")
}
}
|
Output:
Initializing an immutable ListSet
Elements of listSet1 = ListSet(Scala, Article, GeeksForGeeks)
Remove element from the ListSet
After removing element from listset = ListSet(Scala, GeeksForGeeks)
Initialize an empty ListSet :
Scala
import scala.collection.immutable. _
object GFG
{
def main(args : Array[String])
{
println("Initialize an empty ListSet")
val emptyListSet : ListSet[String] = ListSet.empty[String]
println(s"String type empty ListSet = $emptyListSet")
}
}
|
Output:
Initialize an empty ListSet
String type empty ListSet = ListSet()
Note: We can create empty ListSet either by applying the function ListSet.empty or by calling the constructor .
ListSet in Scala is an immutable collection that represents a set of elements as an ordered list. ListSet provides constant time operations for adding and removing elements, and it preserves the order of elements in the list.
Here are some key features and usage examples of ListSet:
- ListSet is immutable, meaning that its contents cannot be modified after creation.
- ListSet provides efficient constant time operations for adding and removing elements, as well as set operations such as union, intersection, and difference.
- ListSet preserves the order of elements in the list, making it useful for scenarios where the order of elements is important.
- ListSet can be used in a variety of scenarios, including removing duplicates from a list, checking for membership of elements in a set, and performing set operations on two or more sets.
Here’s an example code snippet that demonstrates the usage of ListSet in Scala:
Scala
import scala.collection.immutable.ListSet
object ListSetExample {
def main(args : Array[String]) : Unit = {
val listSet = ListSet( 1 , 2 , 3 , 3 , 4 , 5 , 5 )
val updatedSet = listSet + 6
val removedSet = updatedSet - 4
val containsElement = removedSet.contains( 2 )
val otherSet = ListSet( 3 , 4 , 5 , 6 , 7 )
val unionSet = removedSet.union(otherSet)
println( "Original set: " + listSet)
println( "Updated set: " + updatedSet)
println( "Removed set: " + removedSet)
println( "Contains element: " + containsElement)
println( "Union set: " + unionSet)
}
}
|
Output
Original set: ListSet(5, 4, 3, 2, 1)
Updated set: ListSet(6, 5, 4, 3, 2, 1)
Removed set: ListSet(6, 5, 3, 2, 1)
Contains element: true
Union set: ListSet(4, 7, 6, 5, 3, 2, 1)
In this example, we create a new ListSet and add, remove, and check for membership of elements in it. We also get the union of the ListSet with another ListSet. As we can see from the output, the ListSet preserves the order of elements in the list, and the set operations return new sets without modifying the original sets.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
09 Apr, 2023
Like Article
Save Article