Set in Scala | Set-2
Prerequisite: 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 in immutable collection. Example 1:
Scala
// Scala program to illustrate how to // add items using +=, ++== and add() // method in mutable set with mutable // collection import scala.collection.mutable. _ object Main { def main(args : Array[String]) { // Creating and initializing set var myset = Set("G", "Geek", " for ") println("Set before addition "+ "of new elements : ") println(myset) // Adding new element in set // using += and ++== myset + = "Geeks" // Here, "G" is already present in the // Set so, "G" is not added in set myset ++ == List("Geeks 12 ", "geek 23 ", "G") // Adding elements using add() method myset.add("GeeksforGeeks") myset.add("geeksForgeeks 100 ") println("\nSet after addition of new elements : ") println(myset) } } |
Output:
Set before addition of new elements: Set(for, G, Geek) Set after addition of new elements: Set(geek23, for, Geeks, G, Geek, Geeks12, geeksForgeeks100, GeeksforGeeks)
Example 2:
Scala
// Scala program to illustrate how // to add items using += operator in // mutable set with immutable collection import scala.collection.immutable. _ object Main { def main(args : Array[String]) { // Creating and initializing mutable set var myset = Set("G", "Geek", " for ") println("Set before addition" + " of new elements : ") println(myset) // Adding new element in set // using += operator myset + = "GeeksforGeeks" myset + = "geeks 1000 " println("\nSet after addition " + "of new elements : ") println(myset) } } |
Output:
Set before addition of new elements: Set(G, Geek, for) Set after addition of new elements: Set(for, Geek, G, geeks1000, GeeksforGeeks)
Removing elements from the Mutable setIn Set, We can only remove elements in the mutable set. -= and –= methods are used to delete elements and we can also use retain(), clear(), and remove() methods to delete elements when we are working with mutable set in the mutable collection. -= operator is used to delete elements when we are working with mutable set in immutable collection. Example 1:
Scala
// Scala program to illustrate // how to delete items using -= // and --= methods in mutable set // with mutable collection import scala.collection.mutable. _ object Main { def main(args : Array[String]) { // Creating and initializing //mutable set var myset = Set( 100 , 400 , 500 , 600 , 300 , 800 ) println("Set before deletion : ") println(myset) // Deleting elements in set // using -= and --= methods myset - = 600 myset -- = List( 300 , 100 ) println("\nSet after deletion : ") println(myset) } } |
Output:
Set before deletion: Set(300, 100, 800, 500, 600, 400) Set after deletion: Set(800, 500, 400)
Example 2:
Scala
// Scala program to illustrate // how to delete items using // retain(), and clear() methods // in mutable set with mutable // collection import scala.collection.mutable. _ object Main { def main(args : Array[String]) { // Creating and initializing // mutable set var myset 1 = Set( 100 , 400 , 500 , 600 , 300 , 800 ) var myset 2 = Set( 11 , 44 , 55 , 66 , 77 ) println("Set before deletion : ") println(myset 1 ) println(myset 2 ) // Deleting elements in set // using retain() method myset 1 .retain( _ > 500 ) println("\nSet after using retain()" + " method : ") println(myset 1 ) // Deleting elements in set // using clear() method myset 2 . clear println("\nSet after using clear() method : ") println(myset 2 ) } } |
Output:
Set before deletion: Set(300, 100, 800, 500, 600, 400) Set(66, 55, 11, 44, 77) Set after using retain() method: Set(800, 600) Set after using clear() method: Set()
Adding items in immutable SetIn immutable set, We cannot add elements, but we can use + and ++ operators to add element from the immutable set and store the result into a new variable. Here, + is used to add single or multiple elements and ++ is used to add multiple elements defined in another sequence and in concatenation of immutable set. Example:
Scala
// Scala program to illustrate how // to add elements in immutable set import scala.collection.immutable. _ object Main { def main(args : Array[String]) { // Creating and initializing // immutable set val myset 1 = Set( 100 , 400 , 500 , 600 , 300 , 800 ) val myset 2 = Set( 11 , 44 , 55 , 66 , 77 ) println("Set before addition : ") println(myset 1 ) println(myset 2 ) println("\nSet after addition : ") // Add single element in myset1 // and create new Set val S 1 = myset 1 + 900 println(S 1 ) // Add multiple elements in myset1 // and create new Set val S 2 = myset 1 + ( 200 , 300 ) println(S 2 ) // Add another list into myset1 // and create new Set val S 3 = myset 1 ++ List( 700 , 1000 ) println(S 3 ) // Add another set myset2 into // myset1 and create new Set val S 4 = myset 1 ++ myset 2 println(S 4 ) } } |
Output:
Set before addition: Set(500, 600, 800, 300, 400, 100) Set(77, 44, 66, 11, 55) Set after addition: Set(500, 900, 600, 800, 300, 400, 100) Set(500, 600, 800, 300, 400, 200, 100) Set(500, 700, 1000, 600, 800, 300, 400, 100) Set(500, 77, 44, 66, 600, 11, 55, 800, 300, 400, 100)
Removing elements from the immutable setIn immutable set, We cannot remove elements, but we can use – and — operators to remove elements from the immutable set and store the result into a new variable. Here, – operator is used to remove one or more elements and — operator is used to remove multiple elements defined in another sequence. Example:
Scala
// Scala program to illustrate how // to remove elements in immutable set import scala.collection.immutable. _ object Main { def main(args : Array[String]) { // Creating and initializing // immutable set val myset = Set( 100 , 400 , 500 , 600 , 300 , 800 , 900 , 700 ) println("Set before deletion : ") println(myset) println("\nSet after deletion : ") // Remove single element in myset and // Result store into new variable val S 1 = myset - 100 println(S 1 ) // Remove multiple elements from myset // Result store into new variable val S 2 = myset - ( 400 , 300 ) println(S 2 ) // Remove another list from myset // Result store into new variable val S 3 = myset -- List( 700 , 500 ) println(S 3 ) } } |
Output:
Set before deletion: Set(500, 900, 700, 600, 800, 300, 400, 100) Set after deletion: Set(500, 900, 700, 600, 800, 300, 400) Set(500, 900, 700, 600, 800, 100) Set(900, 600, 800, 300, 400, 100)
Set OperationsNow we will see some of the basic mathematical operations on the Set like Union, Intersection, and Difference.
- Union: In this, we could simply add one Set with other. Since the Set will itself not allow any duplicate entries, we need not take care of the common values. To perform union, we use union() method.
- Intersection: To get the common values from both Sets we use intersect() method. It returns a new set which contains all the common values present in both sets.
- Difference: To get the difference of two Sets we use diff() method. It returns the set which contains all the that are not present in myset2.
Example:
Scala
// Scala program to illustrate union, // intersection, and difference on Set import scala.collection.immutable. _ object Main { def main(args : Array[String]) { // Creating and initializing set val myset 1 = Set( 11 , 22 , 33 , 44 , 55 , 66 , 77 , 111 ) val myset 2 = Set( 88 , 22 , 99 , 44 , 55 , 66 , 77 ) // To find intersection val S 1 = myset 1 .intersect(myset 2 ) println("Intersection : ") println(S 1 ) // To find the symmetric difference val S 2 = myset 1 .diff(myset 2 ) println("\nDifference : ") println(S 2 ) // To find union val S 3 = myset 1 .union(myset 2 ) println("\nUnion : ") println(S 3 ) } } |
Output:
Intersection: Set(77, 22, 44, 66, 55) Difference: Set(33, 11, 111) Union: Set(88, 33, 77, 22, 44, 66, 11, 99, 55, 111)
Please Login to comment...