ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Scala program to demonstrate the Set collection

Here, we are going to demonstrate the Set collection in Scala programming language.
Submitted by Nidhi, on June 25, 2021

Problem Solution:

Here, we will demonstrate Set collection. Here we will create an empty set, set of strings, and set of numbers. After that, we will print created sets on the console screen.

Program/Source Code:

The source code to demonstrate Set collection is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to demonstrate the
// Set collection

import scala.collection.immutable._

object Sample {
  // Main method
  def main(args: Array[String]) {
    // An empty set
    val emptySet = Set()

    // Set of cities.
    val cities = Set("DELHI", "MUMBAI", "AGRA", "GWALIOR")

    // Set of cities.
    val luckyNumber = Set(111, 786, 555);

    println("Empty Set: \n\t" + emptySet);
    println("Set of cities: \n\t" + cities);
    println("Lucky number: \n\t" + luckyNumber);
  }
}

Output:

Empty Set: 
	Set()
Set of cities: 
	Set(DELHI, MUMBAI, AGRA, GWALIOR)
Lucky number: 
	Set(111, 786, 555)

Explanation:

Here, we used an object-oriented approach to create the program. And, we imported Collection classes using below statement,

import scala.collection.immutable._

Here, we also created a singleton object Sample and defined the main() function. The main() function is the entry point for the program.

In the main() function, we created an empty set, set of cities, and set of lucky numbers. After that, we printed elements of created sets on the console screen.

Scala Set Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.