Methods to call on a Set in Scala
Last Updated :
12 Jul, 2025
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 :
Scala
// 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)
}
}
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 :
Scala
// 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)
}
}
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 :
Scala
// 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)
}
}
Output:
true
4. def &(that: Set[A]): Set[A] - This method is used to return an intersection of two sets.
Example :
Scala
// 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)
}
}
Output:
Set(8, 4)
5. def &~(that: Set[A]): Set[A] - This symbol means set difference.
Example :
Scala
// 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)
}
}
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 :
Scala
// 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)
}
}
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 :
Scala
// 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)
}
}
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 :
Scala
// 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)
}
}
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 :
Scala
// 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)
}
}
Output:
192784
10. def addString(b: StringBuilder, sep: String): StringBuilder - This method is used a separator to the above functionality.
Example :
Scala
// 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)
}
}
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 :
Scala
// 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)
}
}
Output:
true
12. def drop(n: Int): Set[A]] - This method is used to return all elements except the first n.
Example :
Scala
// 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)
}
}
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 :
Scala
// 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)
}
}
Output:
Set(5, 1, 9, 2)
14. def equals(that: Any): Boolean - This method is used to compares the set to another sequence.
Example :
Scala
// 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)
}
}
Output:
true
15. def head: A - This method is used to return the first element from the set.
Example :
Scala
// 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)
}
}
Output:
5
16. def init: Set[A] - This method is used to return all elements from the set, except the last.
Example :
Scala
// 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)
}
}
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 :
Scala
// 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)
}
}
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 :
Scala
// 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)
}
}
Output:
false
19. def iterator: Iterator[A] - This method help in creating a new iterator over the set.
Example :
Scala
// 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)
}
}
Output:
non-empty iterator
20. def last: A - This method helps in returning the last element from a set.
Example :
Scala
// 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)
}
}
Output:
3
21. def max: A - This method is used to return the highest value from the set.
Example :
Scala
// 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)
}
}
Output:
8
22. def min: A - This method is used to return the lowest element from the set.
Example :
Scala
// 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)
}
}
Output:
1
23. def mkString: String - This method helps to represents all elements of the set as a String.
Example :
Scala
// 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)
}
}
Output:
52738
24. def product: A - This method is used to return the product of all elements in the set.
Example :
Scala
// 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)
}
}
Output:
1890
25. def size: Int - This method is used to return the size of the set.
Example :
Scala
// 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)
}
}
Output:
5
26. def sum: A - This method is used to return the sum of all elements of the set.
Example :
Scala
// 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)
}
}
Output:
27
27. def tail: Set[A] - This method is used to return all elements of the set except the first.
Example :
Scala
// 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)
}
}
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 :
Scala
// 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)
}
}
Output:
Set(5, 1, 9)
Here are some more methods which are called on a set.
Similar Reads
Overview
Basics
Control Statements
Scala | Decision Making (if, if-else, Nested if-else, if-else if)Decision making in programming is similar to decision making in real life. In decision making, a piece of code is executed when the given condition is fulfilled. Sometimes these are also termed as the Control flow statements. Scala uses control statements to control the flow of execution of the prog
5 min read
Scala | Loops(while, do..while, for, nested loops)Looping in programming languages is a feature which facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true. Loops make the programmers task simpler. Scala provides the different types of loop to handle the condition based situation in the progr
5 min read
Break statement in ScalaIn Scala, we use a break statement to break the execution of the loop in the program. Scala programming language does not contain any concept of break statement(in above 2.8 versions), instead of break statement, it provides a break method, which is used to break the execution of a program or a loop
3 min read
Scala | LiteralsAny constant value which can be assigned to the variable is called as literal/constant. The literals are a series of symbols utilized for describing a constant value in the code. There are many types of literals in Scala namely Character literals, String literals, Multi-Line String literals, Boolean
4 min read
OOP Concepts
Methods
Strings
Scala Packages
Scala Trait
Collections
Scala ListsA list is a collection which contains immutable data. List represents linked list in Scala. The Scala List class holds a sequenced, linear list of items. Following are the point of difference between lists and array in Scala: Lists are immutable whereas arrays are mutable in Scala. Lists represents
5 min read
Scala ListBufferA list is a collection which contains immutable data. List represents linked list in Scala. A List is immutable, if we need to create a list that is constantly changing, the preferred approach is to use a ListBuffer. The Scala List class holds a sequenced, linear list of items. A List can be built u
6 min read
ListSet in ScalaA 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 elemen
6 min read
Scala MapMap is a collection of key-value pairs. In other words, it is similar to dictionary. Keys are always unique while values need not be unique. Key-value pairs can have any data type. However, data type once used for any key and value must be consistent throughout. Maps are classified into two types: m
5 min read
Scala | ArraysArray is a special kind of collection in scala. it is a fixed size data structure that stores elements of the same data type. The index of the first element of an array is zero and the last element is the total number of elements minus one. It is a collection of mutable values. It corresponds to arr
6 min read
Scala | ArrayBufferArray in scala is homogeneous and mutable, i.e it contains elements of the same data type and its elements can change but the size of array size canât change. To create a mutable, indexed sequence whose size can change ArrayBuffer class is used. To use, ArrayBuffer, scala.collection.mutable.ArrayBuf
4 min read
Scala | TupleTuple is a collection of elements. Tuples are heterogeneous data structures, i.e., is they can store elements of different data types. A tuple is immutable, unlike an array in scala which is mutable. An example of a tuple storing an integer, a string, and boolean value. val name = (15, "Chandan", tr
5 min read
Set in Scala | Set-1A set is a collection which only contains unique items. The uniqueness of a set are defined by the == method of the type that set holds. If you try to add a duplicate item in the set, then set quietly discard your request. Syntax: // Immutable set val variable_name: Set[type] = Set(item1, item2, ite
3 min read
Set in Scala | Set-2Prerequisite: Set in Scala | Set-1Adding items in Mutable SetIn Set, We can only add new elements in mutable set. +=, ++== and add() method is used to add new elements when we are working with mutable set in mutable collection and += is used to add new elements when we are working with mutable set i
7 min read
BitSet in ScalaA set is a collection which only contains unique items which are not repeatable. A BitSet is a collection of small integers as the bits of a larger integer. Non negative integers sets which represented as array of variable-size of bits packed into 64-bit words is called BitSets. The largest number s
5 min read
HashSet In ScalaHashSet is sealed class. It extends immutable Set and AbstractSet trait. Hash code is used to store elements. It neither sorts the elements nor maintains insertion order . The Set interface implemented by the HashSet class, backed by a hash table . In Scala, A concrete implementation of Set semantic
4 min read
Stack in ScalaA stack is a data structure that follows the last-in, first-out(LIFO) principle. We can add or remove element only from one end called top. Scala has both mutable and immutable versions of a stack. Syntax : import scala.collection.mutable.Stack var s = Stack[type]() // OR var s = Stack(val1, val2, v
3 min read
HashMap in ScalaHashMap is a part of Scala Collection's. It is used to store element and return a map. A HashMap is a combination of key and value pairs which are stored using a Hash Table data structure. It provides the basic implementation of Map. Syntax: var hashMapName = HashMap("key1"->"value1", "key2"->"value
3 min read
TreeSet in ScalaSet is a data structure which allows us to store elements which are unique. The ordering of elements does not guarantee by the Set, than a TreeSet will make elements in a given order. In Scala, TreeSet have two versions: scala.collection.immutable.TreeSet and scala.collection.mutable.TreeSet. Syntax
4 min read
Iterators in ScalaAn iterator is a way to access elements of a collection one-by-one. It resembles to a collection in terms of syntax but works differently in terms of functionality. An iterator defined for any collection does not load the entire collection into the memory but loads elements one after the other. Ther
5 min read
Scala | OptionThe Option in Scala is referred to a carrier of single or no element for a stated type. When a method returns a value which can even be null then Option is utilized i.e, the method defined returns an instance of an Option, in place of returning a single object or a null. Important points : The insta
3 min read