ListSet in Scala

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to Initialize a ListSet 
import scala.collection.immutable._
  
// Creating object 
object GFG 
    // Main method 
    def main(args:Array[String]) 
    
        println("Initializing an immutable ListSet ")
          
        // Creating ListSet
        val listSet1: ListSet[String] = ListSet("GeeksForGeeks",
                                            "Article", "Scala")
        println(s"Elements of listSet1 = $listSet1")
    

chevron_right


Output:

Initializing an immutable ListSet 
Elements of listSet1 = ListSet(Scala, Article, GeeksForGeeks)

Check specific elements in ListSet :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to Check specific elements in ListSet 
import scala.collection.immutable._
  
// Creating object 
object GFG 
    // Main method 
    def main(args:Array[String]) 
    
        println("Initializing an immutable ListSet ")
          
        // Creating ListSet
        val listSet1: ListSet[String] = ListSet("GeeksForGeeks",
                                            "Article", "Scala")
        println(s"Elements of listSet1 = $listSet1")
          
        println("Check elements of immutable ListSet")
          
        // Checking element in a ListSet
        println(s"GeeksForGeeks = ${listSet1("GeeksForGeeks")}")
        println(s"Student = ${listSet1("Student")}")
        println(s"Scala = ${listSet1("Scala")}")
    

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to Add an element in a ListSet
import scala.collection.immutable._
  
// Creating object 
object GFG 
    // Main method 
    def main(args:Array[String]) 
    
        println("Initializing an immutable ListSet ")
          
        // Creating ListSet
        val listSet1: ListSet[String] = ListSet("GeeksForGeeks",
                                            "Article", "Scala")
        println(s"Elements of listSet1 = $listSet1")
          
        // Adding element in ListSet
        println("Add element of immutable ListSet ")
        val listSet2: ListSet[String] = listSet1 + "Java"
        println(s"Adding element java to ListSet $listSet2")
    

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to Add two ListSet
import scala.collection.immutable._
  
// Creating object 
object GFG 
    // Main method 
    def main(args:Array[String]) 
    
        println("Initializing an immutable ListSet ")
          
        // Creating ListSet
        val listSet1: ListSet[String] = ListSet("GeeksForGeeks",
                                               "Article", "Scala")
        println(s"Elements of listSet1 = $listSet1")
          
        // Adding two ListSet
        val listSet2: ListSet[String] = listSet1 ++ ListSet("Java"
                                                            "Csharp")
        println(s"After adding two lists $listSet2")
          
  
    

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to Remove element from the ListSet
import scala.collection.immutable._
  
// Creating object 
object GFG 
    // Main method 
    def main(args:Array[String]) 
    
        println("Initializing an immutable ListSet ")
          
        // Creating ListSet
        val listSet1: ListSet[String] = ListSet("GeeksForGeeks",
                                                "Article", "Scala")
        println(s"Elements of listSet1 = $listSet1")
          
        println("Remove element from the ListSet ")
        val listSet2: ListSet[String] = listSet1 - ("Article")
        println(s"After removing element from listset = $listSet2")
          
  
    

chevron_right


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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program to print empty ListSet
import scala.collection.immutable._
  
// Creating object 
object GFG 
    // Main method 
    def main(args:Array[String]) 
    
        // Creating an empty ListSet
        println("Initialize an empty ListSet")
        val emptyListSet: ListSet[String] = ListSet.empty[String]
        println(s"String type empty ListSet = $emptyListSet")
          
    

chevron_right


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 .



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.