Q:

Scala program to check a Map collection is empty

belongs to collection: Scala Map Programs

0

Here, we will create empty and non-empty Map collections. Then we will check Map collection is empty or not using the isEmpty method and print appropriate messages on the console screen.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to check a Map collection is empty is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to check a 
// Map collection is empty

object Sample {
  // Main method
  def main(args: Array[String]) {
    var employees: Map[String, String] = Map.empty[String, String];
    var students = Map((101, "Amit"), (102, "Arun"), (103, "Anit"));

    if (employees.isEmpty)
      println("Employees-> Empty Map");
    else
      println("Employees-> Non-empty Map");

    if (students.isEmpty)
      println("Students -> Empty Map");
    else
      println("Students -> Non-empty Map");
  }
}

Output:

Employees-> Empty Map
Students -> Non-empty Map

Explanation:

Here, we used an object-oriented approach to create the program. And, 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 two maps studentsemployees. The students map contains the student id and student name. The employee is an empty Map collection. Then we checked Map is empty or not using the isEmpty method and printed the appropriate messages on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Scala program to store key/value pairs using HashM... >>
<< Scala program to delete elements from the Map coll...