Scala Stack filter() method with example Last Updated : 03 Nov, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report In Scala Stack class, the filter() method is utilized to return a new stack that consists of all the elements that satisfy a given predicate. Method Definition: def filter(pred: (A) => Boolean): Stack[A] Return Type: It returns a new stack that consists of all the elements that satisfy a given predicate. Example #1: Scala // Scala program of filter() // 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 filter method val result = s1.filter(x => {x % 2 == 1}) // Display output println("Odd elements: " + result) } } Output: Stack(1, 3, 2, 7, 6, 5) Odd elements: Stack(1, 3, 7, 5) Example #2: Scala // Scala program of filter() // 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 filter method val result = s1.filter(x => {x % 3 == 0}) // Display output println("Elements divisible by 3: " + result) } } Output: Stack(1, 3, 2, 7, 6, 5) Elements divisible by 3: Stack(3, 6) Comment More infoAdvertise with us Next Article Scala Stack find() method with example R rupesh_rao Follow Improve Article Tags : Scala Scala Scala-Method scala-collection Similar Reads Scala Set filter() method with example The filter() method is utilized to select all elements of the set which satisfies a stated predicate. Method Definition: def filter(p: (A) => Boolean): Set[A] Return Type: It returns a set containing all the elements of the set which satisfies the given predicate. Example #1: Scala // Scala progr 1 min read Scala Stack find() method with example 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 2 min read Scala SortedMap filter() method with example The filter() method is utilized to select all elements of the SortedMap which satisfies a stated predicate. Method Definition: def filter(p: ((A, B))=> Boolean): SortedMap[A, B] Return Type: It returns a new SortedMap consisting all the elements of the SortedMap which satisfies the given predicat 1 min read Scala TreeSet filter() method with example In Scala TreeSet class, the filter() method is utilized to return a new TreeSet that consists of all the elements that satisfy a given predicate. Method Definition: def filter(pred: (A) => Boolean): TreeSet[A] Return Type: It returns a new TreeSet that consists of all the elements that satisfy a 2 min read Scala SortedSet filter() method with example The filter() method is utilized to select all elements of the SortedSet which satisfies a stated predicate. Method Definition: def filter(p: (A) => Boolean): SortedSet[A] Return Type: It returns a TreeSet containing all the elements of the SortedSet which satisfies the given predicate. Example #1 1 min read Scala List take() method with example The take() method belongs to the value member of the class List. It is utilized to take the first n elements from the list. Method Definition: deftake(n: Int): List[A] Where, n is the number of elements to be taken from the list. Return Type:It returns a list containing only the first n elements fro 1 min read Like