Scala Stack find() method with example Last Updated : 03 Nov, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report In Scala Stack class, the find() method is utilized to return an element that satisfies a given predicate in the stack. Method Definition: def find(p: (A) => Boolean): Option[A] Return Type: It returns the first element that satisfies a given predicate if present or else it returns None. Example #1: Scala // Scala program of find() // method // Import Stack import scala.collection.mutable._ // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating stack val s1 = Stack(1, 3, 2, 7, 6, 5) // Print the stack println(s1) // Applying find method val result = s1.find(x => {x % 7 == 0}) // Display output println("Element divisible by 7: " + result) } } Output: Stack(1, 3, 2, 7, 6, 5) Element divisible by 7: Some(7) Example #2: Scala // Scala program of find() // method // Import Stack import scala.collection.mutable._ // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating stack val s1 = Stack(1, 3, 2, 7, 6, 5) // Print the stack println(s1) // Applying find method val result = s1.find(x => {x % 10 == 0}) // Display output println("Element divisible by 10: " + result) } } Output: Stack(1, 3, 2, 7, 6, 5) Element divisible by 10: None Comment More infoAdvertise with us Next Article Scala Stack find() method with example R rupesh_rao Follow Improve Article Tags : Scala Scala-Method scala-collection Similar Reads Scala Set find() method with example The find() method is utilized to find the first element of the set that satisfies the given predicate if present. Method Definition: def find(p: (A) => Boolean): Option[A] Return Type: It returns the first element of the set which satisfies the given predicate. Example #1: Scala // Scala program 1 min read Scala SortedMap find() method with example The find() method is utilized to find the first element of the SortedMap that satisfies the given predicate. Method Definition: def find(p: ((A, B)) => Boolean): Option[(A, B)] Return Type: It returns the first element of the SortedMap which satisfies the given predicate. Example #1: Scala // Sca 1 min read Scala TreeSet find() method with example In Scala TreeSet class, the find() method is utilized to return an element that satisfies a given predicate in the TreeSet. Method Definition: def find(p: (A) => Boolean): Option[A] Return Type: It returns the first element that satisfies a given predicate if present or else it returns None. Exam 2 min read Scala Stack exists() method with example In Scala Stack class, the exists() method is utilized to check whether a predicate holds for any of the elements of the stack. Method Definition: def exists(p: (A) => Boolean): Boolean Return Type: It returns true if the predicate holds true for any of the elements of the stack or else returns fa 2 min read Scala Set init() method with example The init() method is utilized to find all the elements of the set except the last one. Method Definition: def init: Set[A] Return Type: It returns all the elements of the set except the last one. Example #1: Scala // Scala program of init() // method // Creating object object GfG { // Main method de 1 min read Like