Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

scala

What is empty in Scala?

Samia Ishaque

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The empty method in Scala is used to empty a map of all its key-value pairs. Below is the definition of the empty method in Scala:

def empty: Map[A, B]

Parameters

The empty method takes no parameters as input.

Return value

The empty method returns an empty map.

Example

Below is an example of how you can use the empty method on maps in Scala. After creating a map of names corresponding to their ages, we first print the map to see what it has stored before emptying it. Then, we use the empty method to empty the map of its contents. This is reflected when we print the map after emptying it.

import scala.compat.Platform.EOL
object Main extends App {
val record1 = Map("Samia"->"20", "Ali"->"25")
println(record1)
val record2 = record1.empty
println(record2)
}

RELATED TAGS

scala

CONTRIBUTOR

Samia Ishaque
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring