## Scala Set ,Union and Intersect

Category : Scala | Sub Category : Scala Programs | By Prasad Bonam Last updated: 2020-10-10 03:43:28 Viewed : 579

Scala Set ,Union and Intersect

In Scala, you can perform set operations like union and intersection on sets to combine or extract common elements from different sets. Here is how you can use `Set` for these operations:

1. Creating Sets:

First, lets create two sets to work with:

```scalaval set1 = Set(1, 2, 3, 4, 5)
val set2 = Set(3, 4, 5, 6, 7)```

2. Union of Sets:

The union of two sets contains all unique elements from both sets. You can use the `union` method or the `|` operator for this operation:

Using `union` method:

`scalaval unionSet = set1.union(set2)`

Using `|` operator:

```scala```val unionSet = set1 | set2
``````

3. Intersection of Sets:

The intersection of two sets contains only the elements that are common to both sets. You can use the `intersect` method or the `&` operator for this operation:

Using `intersect` method:

```scala```val intersectedSet = set1.intersect(set2)
``````

Using `&` operator:

```scala```val intersectedSet = set1 & set2
``````

4. Output:

Here is how you can print the results:

```scalaprintln("Set 1: " + set1)
println("Set 2: " + set2)
println("Union Set: " + unionSet)
println("Intersection Set: " + intersectedSet)```

5. Complete Example:

Putting it all together:

```scalaval set1 = Set(1, 2, 3, 4, 5)
val set2 = Set(3, 4, 5, 6, 7)

val unionSet = set1 | set2
val intersectedSet = set1 & set2

println("Set 1: " + set1)
println("Set 2: " + set2)
println("Union Set: " + unionSet)
println("Intersection Set: " + intersectedSet)```

Output:

```mathematicaSet 1: Set(1, 2, 3, 4, 5)
Set 2: Set(3, 4, 5, 6, 7)
Union Set: Set(1, 2, 3, 4, 5, 6, 7)
Intersection Set: Set(3, 4, 5)```

In this example, we create two sets, perform the union operation using both methods, and then perform the intersection operation using both methods. Finally, we print the results to demonstrate the union and intersection of sets in Scala.

Computes the intersection between this set and another set.

def intersect(that: GenSet[Int]): Set[Int]

(OR)

def &(that: GenSet[Int]): Set[Int]

Computes the union between this set and another set.

def union(that: GenSet[Int]): Set[Int]

Set API List:

Example:

Following example illustrates about Scala set, union and interection

Save the file as −  SetAppl.scala

SetAppl.scala

` package runnerdev `

object SetAppl extends App {

/**Scala program that uses set, contains*/

// Create a Set of two strings.

val animals = Set("cat""dog")

println("animals :" + animals)

// See if this string is in the set.

if (animals.contains("dog"))

println(true)

//Scala program that combines two sets

// Create two sets.

val results1 = Set(124)

val results2 = Set(235)

/** "++" operator to combine two sets **/

val combSet = results1 ++ results2

println("combSet :" + combSet)

val combSet1 = results1.union(results2)

// Display all sets.

println(results1)

println(results2)

println(combSet)

println(combSet1)

// Use intersect to find common elements of two sets.

val interSec = results1.intersect(results2)

println("interSec : " + interSec)

// Short syntax for intersection.

val interSec1 = results1 & results2

println("interSec1: " + interSec1)

}

compile and run the above example as follows

scala> scalac SetAppl.scala

scala> scala SetAppl

animals :Set(cat, dog)

true

combSet :Set(5, 1, 2, 3, 4)

Set(1, 2, 4)

Set(2, 3, 5)

Set(5, 1, 2, 3, 4)

Set(5, 1, 2, 3, 4)

interSec : Set(2)

interSec1: Set(2)