Methods to call on a Set in Scala

A set is a collection that only contains unique items. In Scala, both mutable and immutable sets are available. The mutable set is those set in which the value of the object is change, but, in the immutable set, the value of the object is not changed itself. The immutable set is defined under Scala.collection.immutable._ package and mutable set are defined under Scala.collection.mutable._ package. Here, we will discuss some important methods of Scala.collection.immutable._ package.

1. def +(elem: A): Set[A] – This method is used to add an element to the set and then returns it.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of +() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(8, 9, 1, 4, 2, 7
          
        // Applying +() method 
        val result = s.+(6
              
        // Display output 
        print(result)     
    

chevron_right


Output:

Set(1, 6, 9, 2, 7, 8, 4)

2. def -(elem: A): Set[A] – This method is used to remove the element from the set and then returns it.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of -() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(8, 9, 1, 4, 2, 7
          
        // Applying -() method 
        val result = s.-(1
              
        // Display output 
        print(result)     
    

chevron_right


Output:



Set(9, 2, 7, 8, 4)

3. def contains(elem: A): Boolean – This method is return true if the set contains the specified element. Otherwise, it will return false.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of contains() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(8, 9, 1, 4, 2, 7
          
        // Applying contains() method 
        val result = s.contains(1
              
        // Display output 
        print(result)     
    

chevron_right


Output:

true

4. def &(that: Set[A]): Set[A] – This method is used to return an intersection of two sets.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of &() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s1 = Set(8, 9, 1, 4, 2, 7)
        val s2 = Set(3, 4, 6, 8
          
        // Applying &() method 
        val result = s1.&(s2)
              
        // Display output 
        print(result)     
    

chevron_right


Output:

Set(8, 4)

5. def &~(that: Set[A]): Set[A] – This symbol means set difference.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of &~() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s1 = Set(8, 9, 1, 4, 2, 7)
        val s2 = Set(3, 4, 6, 8
          
        // Applying &~() method 
        val result = s1.&~(s2)
              
        // Display output 
        print(result)     
    

chevron_right


Output:

Set(1, 9, 2, 7)

6. def +(elem1: A, elem2: A, elems: A*): Set[A] – This method is used to add the multiple elements to a Scala set and then returns it.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of +() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(8, 9, 1, 4, 2, 7)
          
        // Applying +() method 
        val result = s.+(3, 4, 6
              
        // Display output 
        print(result)     
    

chevron_right


Output:



Set(1, 6, 9, 2, 7, 3, 8, 4)

7. def ++(elems: A): Set[A] – This method is used for concatenation of a set with another.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of ++() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s1 = Set(8, 9, 1, 4, 2, 7)
        val s2 = Set(3, 4, 6
          
        // Applying ++() method 
        val result = s1 ++ s2 
              
        // Display output 
        print(result)     
    

chevron_right


Output:

Set(1, 6, 9, 2, 7, 3, 8, 4)

8. def -(elem1: A, elem2: A, elems: A*): Set[A] – This method is used to remove each element mentioned from the set.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of -() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(8, 9, 1, 4, 2, 7)
          
        // Applying -() method 
        val result = s.-(8, 1, 3
              
        // Display output 
        print(result)     
    

chevron_right


Output:

Set(9, 2, 7, 4)

9. def addString(b: StringBuilder): StringBuilder – This method is used to add all the elements of the set to the String Builder.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of addString() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(8, 9, 1, 4, 2, 7)
          
        // Applying addString() method 
        val result = s.addString(new StringBuilder()) 
              
        // Display output 
        print(result)     
    

chevron_right


Output:

192784

10. def addString(b: StringBuilder, sep: String): StringBuilder – This method is used a separator to the above functionality.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of addString() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(8, 9, 1, 4, 2, 7)
          
        // Applying addString() method 
        val result = s.addString(new StringBuilder(), "*"
              
        // Display output 
        print(result)     
    

chevron_right


Output:

1*9*2*7*8*4

11. def apply(elem: A) – This method is used to checks whether the element is part of the set or not. It returns Boolean value True or False.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of apply() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(8, 9, 1, 4, 2, 7)
          
        // Applying apply() method 
        val result = s.apply(2)
              
        // Display output 
        print(result)     
    

chevron_right


Output:



true

12. def drop(n: Int): Set[A]] – This method is used to return all elements except the first n.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of drop() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(2, 3, 5, 7, 8)
          
        // Applying drop() method 
        val result = s.drop(2)
              
        // Display output 
        print(result)     
    

chevron_right


Output:

Set(7, 3, 8)

13. def dropRight(n: Int): Set[A] – This method is used to returns all elements except the last n that is just opposite to the above method().
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of dropRight() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(1, 2, 3, 5, 7, 9)
          
        // Applying dropRight() method 
        val result = s.dropRight(2)
              
        // Display output 
        print(result)     
    

chevron_right


Output:

Set(5, 1, 9, 2)

14. def equals(that: Any): Boolean – This method is used to compares the set to another sequence.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of equals() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(2, 3, 5, 7, 8)
          
        // Applying equals() method 
        val result = s.equals(Set(2, 3, 5, 7, 8))
              
        // Display output 
        print(result)     
    

chevron_right


Output:

true

15. def head: A – This method is used to return the first element from the set.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of head() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(1, 2, 3, 5, 7, 9)
          
        // Applying head() method 
        val result = s.head
              
        // Display output 
        print(result)     
    

chevron_right


Output:

5

16. def init: Set[A] – This method is used to return all elements from the set, except the last.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of init() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(1, 2, 3, 5, 7, 9)
          
        // Applying init() method 
        val result = s.init
              
        // Display output 
        print(result)     
    

chevron_right


Output:



Set(5, 1, 9, 2, 7)

17. def intersect(that: Set[A]): Set[A] – This method is used to return the intersection of two sets that is it returns the common element from the two sets.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of intersect() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s1 = Set(1, 2, 3, 5, 7, 9)
        val s2 = Set(1, 2, 3, 4, 9, 9)
          
        // Applying intersect() method 
        val result = s1.intersect(s2
              
        // Display output 
        print(result)     
    

chevron_right


Output:

Set(1, 9, 2, 3)

18. def isEmpty: Boolean – This method return true if the given set is empty. Otherwise, it will return False.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of isEmpty() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(2, 3, 5, 7, 8)
          
        // Applying isEmpty() method 
        val result = s.isEmpty 
              
        // Display output 
        print(result)     
    

chevron_right


Output:

false

19. def iterator: Iterator[A] – This method help in creating a new iterator over the set.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of iterator() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(2, 3, 5, 7, 8)
          
        // Applying iterator() method 
        val result = s.iterator
              
        // Display output 
        print(result)     
    

chevron_right


Output:

non-empty iterator

20. def last: A – This method helps in returning the last element from a set.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of last() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(1, 2, 3, 5, 7, 9)
          
        // Applying last() method 
        val result = s.last
              
        // Display output 
        print(result)     
    

chevron_right


Output:

3

21. def max: A – This method is used to return the highest value from the set.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of max() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(2, 3, 5, 7, 8)
          
        // Applying max() method 
        val result = s.max 
              
        // Display output 
        print(result)     
    

chevron_right


Output:



8

22. def min: A – This method is used to return the lowest element from the set.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of min() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(1, 2, 3, 5, 7, 9)
          
        // Applying min() method 
        val result = s.min 
              
        // Display output 
        print(result)     
    

chevron_right


Output:

1

23. def mkString: String – This method helps to represents all elements of the set as a String.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of mkString() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(2, 3, 5, 7, 8)
          
        // Applying mkString() method 
        val result = s.mkString
              
        // Display output 
        print(result)     
    

chevron_right


Output:

52738

24. def product: A – This method is used to return the product of all elements in the set.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of product() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(1, 2, 3, 5, 7, 9)
          
        // Applying product() method 
        val result = s.product
              
        // Display output 
        print(result)     
    

chevron_right


Output:

1890

25. def size: Int – This method is used to return the size of the set.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of size() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(2, 3, 5, 7, 8)
          
        // Applying size() method 
        val result = s.size 
              
        // Display output 
        print(result)     
    

chevron_right


Output:

5

26. def sum: A – This method is used to return the sum of all elements of the set.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of sum() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(1, 2, 3, 5, 7, 9)
          
        // Applying sum() method 
        val result = s.sum
              
        // Display output 
        print(result)     
    

chevron_right


Output:

27

27. def tail: Set[A] – This method is used to return all elements of the set except the first.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of tail() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(2, 3, 5, 7, 8)
          
        // Applying tail() method 
        val result = s.tail
              
        // Display output 
        print(result)     
    

chevron_right


Output:

Set(2, 7, 3, 8)

28. def take(n: Int): Set[A] – This method is used to return the first n elements from the set.
Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of take() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
          
        // Creating a set 
        val s = Set(1, 2, 3, 5, 7, 9)
          
        // Applying take() method 
        val result = s.take(3)
              
        // Display output 
        print(result)     
    

chevron_right


Output:

Set(5, 1, 9)

Here are some more methods which are called on a set.

Method Description
def &(that: Set[A]): Set[A] This method helps in returning an intersection of two sets.
def count(p: (A) => Boolean): Int This method is used to return the count of elements that satisfy the predicate.
def diff(that: Set[A]): Set[A] This method is used to return the set difference(elements existing in one set, but not in another).
def dropWhile(p: (A) => Boolean): Set[A] This method is used to drop elements until the first element that doesn’t satisfy the predicate.
def exists(p: (A) => Boolean): Boolean This method is used to return true if predicate holds true else returns false.
def filter(p: (A) => Boolean): Set[A] This method is used to filter elements.
def find(p: (A) => Boolean): Option[A] This method is used to return the first element that satisfies the predicate.
def forall(p: (A) => Boolean): Boolean This method is used to return true if all elements of the set satisfy the predicate. Otherwise, false.
def map[B](f: (A) => B): immutable.Set[B] This method is used to applies the function to all elements of the set and returns it.
def splitAt(n: Int): (Set[A], Set[A]) This method is used to split the set at the given index and returns the two resulting subsets.
def subsetOf(that: Set[A]): Boolean This method is used to return true if the set passed as argument is a subset of this set else return false.
def takeRight(n: Int):Set[A] This method is used to return the last n elements.
def toArray: Array[A] This method is used to return an Array holding elements from the set.
def toList: List[A] This method is used to return a List from elements of the set.
def toSeq: Seq[A] This method is used to return a sequence from the set.
def toString(): String This method represents the elements of the set as a String.



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 :

1


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