Open In App

Scala Set map() method with example

Last Updated : 18 Oct, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The map() method is utilized to build a new set by applying a function to all elements of this set.

Method Definition: def map[B](f: (A) => B): immutable.Set[B]

Return Type: It returns a new set containing all the elements after applying the given function.

Example #1:




// Scala program of map() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
        // Creating a set 
        val s1 = Set(5, 1, 3, 2, 4
          
        // Applying map method 
        val result = s1.map(x => x*x)
          
        // Display output
        println(result)
    


Output:

Set(25, 1, 9, 16, 4)

Example #2:




// Scala program of map() 
// method 
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
        // Creating a set 
        val s1 = Set(5, 1, 3, 2, 4
          
        // Applying map method 
        val result = s1.map(x => x/2)
          
        // Display output
        println(result)
    


Output:

Set(2, 0, 1)


Similar Reads

Scala Map size() method with example
The size() is utilized to find the number of key-value pairs in the stated map. Method Definition: def size: Int Return Type: It returns the number of elements in the map. Example #1: // Scala program of size() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a map val m1 = Map(3 -> "geeks
1 min read
Scala Map toSet() method with example
The toSet() method is utilized to display a set from the Scala map. Method Definition: def toSet: Set[A] Return Type: It returns a set from the stated map. Example #1: // Scala program of toSet() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a map val m1 = Map(3 -> "geeks", 4 ->
1 min read
Scala map contains() method with example
The contains() method of Scala is equivalent to the isDefinedAt method of Scala but the only difference is that isDefinedAt is observed on all the PartialFunction classes while contains is clearly defined to the Map interface of Scala. It checks whether the stated map contains a binding for a key or not. Method Definition: def contains(key: K): Boo
2 min read
Scala map isDefinedAt() method with example
The method isDefinedAt() is an abstract value member of trait PartialFunction, which is identical to the method contains and is observed in all the classes of PartialFunction. It inspects whether a value is contained in the function's domain or not. Method Definition: abstract def isDefinedAt(x: A): Boolean where, x is the value to be tested. Retur
2 min read
Scala Iterator map() method with example
The map() method belongs to the concrete value member of the class Abstract Iterator. It is utilized to build a new iterator by applying a function to each of the elements of the stated iterator. Method Definition: def map[B](f: (A) => B): Iterator[B] where, B is the element type of the returned iterator and f is the function to be applied on ea
2 min read
Scala Map clone() method with example
The clone() method is utilized to make a copy of the receivers object. value clone is a member of scala.collection.mutable.Map[String, Int]. Method Definition: def clone(): Map[A, B] Return Type: It returns the copy of the map used. Example #1: // Scala program of clone() // method // Creating object object GfG { // Main method def main(args:Array[
1 min read
Scala Map clear() method with example
The clear() method is utilized to clear the map. value clear is a member of scala.collection.mutable.Map[String, Int]. Method Definition: def clear(): Unit Return Type: It returns empty map. Example #1: // Scala program of clear() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a mutable map val m
1 min read
Scala Map apply() method with example
The apply() is utilized to search a key in the stated map. Method Definition: m1.apply("key") Here m1 is map. Return Type: It returns the value of the key to be searched. Example #1: // Scala program of apply() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a map val m1 = Map("geeks" -
1 min read
Scala Map addString() method with a separator with example
This method is identical to the addString() method but here a separator is also included. Method Definition: def addString(b: StringBuilder, sep: String): StringBuilder Where, sep is the separator stated. Return Type: It returns the elements of the map in the String Builder and a separator is also added between the elements of the map. Example #1:
1 min read
Scala Map addString() method with example
The addString() method is utilized to add the elements of the map to the String Builder. Method Definition: def addString(b: StringBuilder): StringBuilder Return Type: It returns the elements in the String Builder. Example #1: // Scala program of addString() // method // Creating object object GfG { // Main method def main(args:Array[String]) { //
1 min read