Home » Scala language

Scala Set Functions with Examples

Scala Set Functions: In this tutorial, we are going to learn about some of the most used functions of sets in Scala with examples.
Submitted by Shivang Yadav, on November 30, 2019

Scala Set Functions

Scala programming language has many functions that work on sets in Scala. In this tutorial on Scala, we learn about functions that will operate on sets in Scala.

A set is a special type of collection which contains all unique elements. In Scala many functions are available to for manipulating data in Scala.

Let's see some of these functions in Scala,

1) Adding elements in set (on mutable set)

In Scala, some functions can be used to over mutable sets to perform addition operation.

  • +=: This operator is used to add new elements to a mutable set in the immutable collection.
  • ++==: This operator is used to add new elements to a mutable set.
  • add(): This method is used to add new elements to a mutable set in an immutable collection.

Let's take a few examples to understand the working of the code,

import scala.collection.mutable._

object Main 
{ 
	def main(args: Array[String]) 
	{ 
		
		var myBikes = Set("Ninja 300" , "Thunderbird 350", "Dominar 400") 
		println("My bike collection is : ")
		println(myBikes) 
		
		print("Adding a new bike to my collection, ")
		myBikes += "Continental GT"
		println("My bike collection now is: ")
		println(myBikes) 
		
		println("Adding some new bikes to my collection, ")
		myBikes ++== List("Bajaj Platina", "Splender")
		println("My bike collection now is: ")
		println(myBikes)
		
		print("Adding a new bike to my collection, ")
 		myBikes.add("Hayabusa")
 		println("My bike collection now is: ")
		println(myBikes) 
	} 
} 

Output

My bike collection is : 
HashSet(Thunderbird 350, Dominar 400, Ninja 300)
Adding a new bike to my collection, My bike collection now is: 
HashSet(Thunderbird 350, Continental GT, Dominar 400, Ninja 300)
Adding some new bikes to my collection, 
My bike collection now is: 
HashSet(Splender, Bajaj Platina, Dominar 400, Ninja 300, Thunderbird 350, Continental GT)
Adding a new bike to my collection, My bike collection now is: 
HashSet(Splender, Hayabusa, Bajaj Platina, Dominar 400, Ninja 300, Thunderbird 350, Continental GT)

2) Adding elements in set (On immutable elements)

In the case of an immutable set, we cannot add elements to the existing set. But we can add elements with an immutable set and store this into a new variable. For these two operators can be used,

  • +: This operator is used to add one element to the set and then store it into another variable.
  • ++: This operator is used to add multiple elements to the set and then store this sum into another variable.

Example:

import scala.collection.immutable._
object Main 
{ 
	def main(args: Array[String]) 
	{ 
	    val myBikes = Set("R1" , "Thunderbird" , "GS390")
	    val FBikes = Set("Bullet 350", "Continental GT" , "K1000")
	    
		println("My Bike collection: ") 
		println(myBikes) 
		println("My friends Bike collection: ") 
		println(FBikes) 
		
		println("Adding one more bike to friends collection ")
		var FBikes2 = FBikes + "Ninja 300"
		println("My friends Bike collection now is: ") 
		println(FBikes2) 
		
		println("Adding  more bikes to my collection ")
		var myBikes2 = myBikes ++ List("CBR 1000R" ,"R1100R" )
		println("My friends Bike collection now is: ") 
		println(myBikes2) 
		
		println("Our collection togather has: ")
		var bike = myBikes2 ++ FBikes2
		println(bike)
	} 
} 

Output

My Bike collection: 
Set(R1, Thunderbird, GS390)
My friends Bike collection: 
Set(Bullet 350, Continental GT, K1000)
Adding one more bike to friends collection 
My friends Bike collection now is: 
Set(Bullet 350, Continental GT, K1000, Ninja 300)
Adding  more bikes to my collection 
My friends Bike collection now is: 
HashSet(R1100R, GS390, Thunderbird, R1, CBR 1000R)
Our collection togather has: 
HashSet(R1100R, Ninja 300, GS390, K1000, Continental GT, Thunderbird, Bullet 350, R1, CBR 1000R)

3) Removing elements from sets (on mutable set)

In Scala programming language removing elements from a set is valid. There are operators as well as methods that can be used to perform the task.

  • -=: this operator is used to delete a single element from the set.
  • --=: this operator is used to delete multiple elements from the set.

Methods retain(), clear(), remove() are used to delete elements from mutable set in mutable collection.

Example:

import scala.collection.mutable._

object Main 
{ 
	def main(args: Array[String]) 
	{ 
		
		var myBikes = Set("Ninja 300" , "Thunderbird 350", "Dominar 400", "Continental GT", "S1000RR") 
		println("My bike collection is: ")
		println(myBikes) 
		
		println("Removing dominar400 from my collection : ")
		myBikes -= "Dominar 400"
		println("My Collection is : ")
		println(myBikes)
		
		println("Removing few more bikes from my collection : ")
		myBikes --== List("S1000RR" , "Ninja 300") 
		println("My Collection is : ")
		println(myBikes)
	}  
}

Output

My bike collection is: 
HashSet(Dominar 400, Ninja 300, Thunderbird 350, Continental GT, S1000RR)
Removing dominar400 from my collection : 
My Collection is : 
HashSet(Ninja 300, Thunderbird 350, Continental GT, S1000RR)
Removing few more bikes from my collection : 
My Collection is : 
HashSet(Thunderbird 350, Continental GT)

4) Removing elements from sets (on immutable set)

In the case of immutable sets, we cannot normally remove the elements from the set. We need to remove them from a set and then store the updated set to a new memory location. Operators - and -- are valid for removing elements from the set.

Example:

import scala.collection.immutable._

object Main 
{ 
	def main(args: Array[String]) 
	{ 
		
		val myBikes = Set("Ninja 300" , "Thunderbird 350", "Dominar 400", "Continental GT", "S1000RR") 
		println("My bike collection is : ")
		println(myBikes) 

		println("Removing dominar400 from my collection: ")
		val myBikes2 = myBikes - "Dominar 400"
		println("My Collection is: ")
		println(myBikes2)

		println("Removing few more bikes from my collection: ")
		val myBikes3 = myBikes2 -- List("S1000RR" , "Ninja 300") 
		println("My Collection is: ")
		println(myBikes3)
	}  
}

Output

My bike collection is : 
HashSet(Ninja 300, Continental GT, Dominar 400, S1000RR, Thunderbird 350)
Removing dominar400 from my collection: 
My Collection is: 
HashSet(Ninja 300, Continental GT, S1000RR, Thunderbird 350)
Removing few more bikes from my collection: 
My Collection is: 
HashSet(Continental GT, Thunderbird 350)



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.