Monday, 6 May 2019

Set in Scala



 
Set
Scala Set is a collection of pairwise different elements of the same type. In other words, a Set is a collection that contains no duplicate elements. There are two kinds of Sets, the immutable and themutable. The difference between mutable and immutable objects is that when an object is immutable, the object itself can't be changed.
By default, Scala uses the immutable Set. If you want to use the mutable Set, you'll have to import scala.collection.mutable.Set class explicitly. If you want to use both mutable and immutable sets in the same, then you can continue to refer to the immutable Set as Set but you can refer to the mutable Set as mutable.Set
// Empty set of integer type
 var s : Set[Int] = Set()
// Set of integer type
var s : Set[Int] = Set(1,3,5,7)
or
var s = Set(1,3,5,7)

Basic Operations on Set:
Head àThis method returns the first element of a set.
Tail àThis method returns a set consisting of all elements except the first.
isEmpty à This method returns true if the set is empty otherwise false.
val fruit = Set("apples", "oranges", "pears")
val nums: Set[Int] = Set()
println( "Head of fruit : " + fruit.head )
println( "Tail of fruit : " + fruit.tail )
println( "Check if fruit is empty : " + fruit.isEmpty )
println( "Check if nums is empty : " + nums.isEmpty )
Output :
Head of fruit : apples
Tail of fruit : Set(oranges, pears)
Check if fruit is empty : false
Check if nums is empty : true
//
val num = Set(5,6,9,20,30,45)
println(num.max)  //45
println(num.min) //5
var myset2 = Set(10, 100, 1000, 10000, 100000) 
println("\nSet 2:") 
myset2.foreach((item:Int)=>println(item))
Output :
Set 2: 10 100000 10000 1000 100
var myset = Set("G", "Geek", "for") // Creating and initilazing set
myset += "Geeks" // using += and ++==
myset ++== List("Geeks12", "geek23", "G")

Get sorted values from Set

import scala.collection.immutable.SortedSet
val myset: SortedSet[Int] = SortedSet(87, 0, 3, 45, 7, 56, 8,6) 
myset.foreach((items: Int)=> println(items)) 
var myset = Set(100, 400, 500, 600, 300, 800) 
// Deleting elements in set
// using -= and --= methods
myset -= 600
myset --= List(300, 100)
Output:
Set before deletion:
Set(300, 100, 800, 500, 600, 400)
Set after deletion:
Set(800, 500, 400)
// using clear() method
myset2. clear
var myset1 = Set(100, 400, 500,600,300, 800)
// using retain() method
myset1.retain(_>500)
Output :
Set(800, 600)
// Creating and initializing set
val myset1 = Set(11, 22, 33, 44,55, 66, 77, 111)
val myset2 = Set(88, 22, 99, 44,55, 66, 77)
val S1 = myset1.intersect(myset2)
println(S1)
Output:
Set(77, 22, 44, 66, 55)

// To find the symmetric difference
val S2 = myset1.diff(myset2)
println(S2)
Output:
Set(33, 11, 111)

// To find union
val S3 = myset1.union(myset2)
println(S3)
Output:
Set(88, 33, 77, 22, 44, 66, 11, 99, 55, 111)

Note : Set display value in random order.

No comments:

Post a Comment

Use filter method to filter a Scala collection

Use filter method to filter a Scala collection To use filter on your collection, give it a predicate to filter the collection elements as...