Home » 
        Scala
    
    SortedMap find() Method in Scala
    
    
    
    
        
            By IncludeHelp Last updated : November 14, 2024
        
    
    Map is a collection that stores its elements as key-value pairs, like a dictionary. SortedMap is a special type of map in which elements are sorted in ascending order.
    
    SortedMap find() Method
    The find() method on SortedMap is used to find the first element from the map that satisfies the given condition in the parameter of the method.
Syntax
sortedmap_Name.find(condition)
    Parameters
    The method accepts a condition.
    Return Type
    It returns an element that satisfies the given condition.
    Example 1
    Program to illustrate the working of find() method
import scala.collection.SortedMap
object MyClass {
    def main(args: Array[String]): Unit = {
        val mySortedMap = SortedMap("scala" -> 5, "JavaScript" -> 6, "Ruby" -> 8)
        println("Sorted Map: " + mySortedMap)
    
        val isPresentEle1 = mySortedMap.find(ele => ele._2 == 5)
        println("Element with value 4 present " + isPresentEle1)
    
        val isPresentEle2 = mySortedMap.find(ele => ele._2 == 3)
        println("Element with value 3 present " + isPresentEle2)
    }
}
Output
Sorted Map: TreeMap(JavaScript -> 6, Ruby -> 8, scala -> 5)
Element with value 4 present Some((scala,5))
Element with value 3 present None
    
    
  
    Advertisement
    
    
    
  
  
    Advertisement