Home » Scala Set

Scala Set

It is used to store unique elements in the set. It does not maintain any order for storing elements. You can apply various operations on them. It is defined in the Scala.collection.immutable package.


Scala Set Syntax

Scala Set Example

In this example, we have created a set. You can create an empty set also. Let’s see how to create a set.

Output:

Set()// an empty set Set(Cricket,Football,Hocky,Golf) 

Scala Set Example 2

In Scala, Set provides some predefined properties to get information about set. You can get first or last element of Set and many more. Let’s see an example.

Output:

Cricket Set(Football, Hocky, Golf) false 

Scala Set Example: Merge two Set

You can merge two sets into a single set. Scala provides a predefined method to merge sets. In this example, ++ method is used to merge two sets.

Output:

Elements in games set: 4 Elements in alphabet set: 5 Elements in mergeSet: 9 Set(E, Football, Golf, Hocky, A, B, C, Cricket, D) 

This example also proves that the merge set does not maintain order to store elements.


Scala Set Example 2

You can check whether element is present in the set or not. The following example describe the use of contains() method.

Output:

Set(Cricket, Football, Hocky, Golf) Elements in set: 4 Golf exists in the set : true Racing exists in the set : false 

Scala Set Example: Adding and Removing Elements

You can add or remove elements from the set. You can add only when your code is mutable. In this example, we are adding and removing elements of the set.

Output:

Set(Cricket, Football, Hocky, Golf) Set(Football, Golf, Hocky, Cricket, Racing) Set(Football, Golf, Hocky, Cricket, Racing) Set(Football, Hocky, Cricket, Racing) 

Scala Set Example: Iterating Set Elements using for loop

You can iterate set elements either by using for loop or foreach loop. You can also filter elements during iteration. In this example have used for loop to iterate set elements.

Output:

Cricket Football Hocky Golf 

Scala Set Example Iterating Elements using foreach loop

In this example, we are using foreach loop to iterate set elements.

Output:

Cricket Football Hocky Golf 

Scala Set Example: Set Operations

In scala Set, you can also use typical math operations like: intersection and union. In the following example we have used predefined methods to perform set operations.

Output:

Intersection by using intersect method: Set(Golf, C) Intersection by using & operator: Set(Golf, C) Set(E, Football, Golf, Hocky, A, B, C, Cricket, D) 

Scala SortedSet

In scala, SortedSet extends Set trait and provides sorted set elements. It is useful when you want sorted elements in the Set collection. You can sort integer values and string as well.

It is a trait and you can apply all the methods defined in the traversable trait and Set trait.

Scala SortedSet Example

In the following example, we have used SortedSet to store integer elements. It returns a Set after sorting elements.

Output:

1 2 4 5 6 7 8 9 
Next TopicScala HashSet

You may also like