×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Scala Set Tutorial with Examples

By IncludeHelp Last updated : November 14, 2024

Scala Set

A set is a collection of unique items stored. In this tutorial on Scala sets, we will learn about sets in Scala along with the working code.

A set is a collection of unique elements. If a duplicate item is added to the set the item will be discarded.

Example: {1, 4, 6, 7}

Syntax

// for creating immutable set,
val set_name : Set[type]  = Set(items...)
val set_name = Set[type] = Set(items...)

// for creating mutable set,
var set_name : Set[type] = Set(items...)
var set_name = Set(items...)

Sets in Scala can be mutable and immutable too. If a Scala set is mutable (declared from the package: Scala.collection.mutable), the values of its elements can be changed anywhere in the program. For immutable sets (declared from the package: Scala.collection.immutable) this value cannot be changed after initialization. The default declaration of Scala sets will create an immutable object.

The creation of an empty Scala set is also valid. And on the set methods like add, remove, clear, etc are applicable and are in the Scala library.

Example 1: Declaration of immutable set

import scala.collection.immutable._
  
object Main  
{ 
    def main(args: Array[String]): Unit = {      
        val Bikes: Set[String] = Set("ThunderBird", "Classic 350 ", "Continental GT", "Interceptor", "Himalayan")
        val Score = Set(78, 67, 90, 56, 29)
          
        println("Bikes from Royal Enfield: ") 
        println(Bikes) 
          
        println("Score in different subjects: ") 
        for(myset<-Score) 
        { 
            println(myset) 
        } 
    } 
}

Output

Bikes from Royal Enfield: 
HashSet(ThunderBird, Interceptor, Classic 350 , Continental GT, Himalayan)
Score in different subjects: 
56
67
90
78
29

Example 2: Creating a mutable set

import scala.collection.mutable._  
object Main  
{ 
    def main(args: Array[String]): Unit = {
        var Bikes: Set[String] = Set("ThunderBird", "Classic 350 ", "Continental GT", "Interceptor", "Himalayan")
        var Score = Set(78, 67, 90, 56, 29)
          
        println("Bikes from Royal Enfield: ") 
        println(Bikes) 
          
        println("Score in different subjects: ") 
        for(myset<-Score) 
        { 
            println(myset) 
        } 
    } 
} 

Output

Bikes from Royal Enfield: 
HashSet(Continental GT, ThunderBird, Himalayan, Classic 350 , Interceptor)
Score in different subjects: 
67
56
90
29
78

Example 3: Creating an empty Sets in Scala

import scala.collection.mutable._  

object Main  
{ 
    def main(args: Array[String]): Unit = {
        val empty = Set() 
        print("An empty Set : ") 
        println(empty)
    } 
}

Output

An empty Set : HashSet()

Comments and Discussions!

Load comments ↻





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