Scala Queue filter() method with example Last Updated : 24 Oct, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report The filter() method is utilized to return a new queue that consists of all the elements that satisfy a given predicate. Method Definition: def filter(pred: (A) => Boolean): Queue[A] Return Type: It returns a new queue that consists of all the elements that satisfy a given predicate. Example #1: Scala // Scala program of filter() // method // Import Queue import scala.collection.mutable._ // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating queues val q1 = Queue(1, 3, 2, 7, 6, 5) // Print the queue println(q1) // Applying filter method val result = q1.filter(x => {x % 2 == 1}) // Displays output print("Odd elements: " + result) } } Output: Queue(1, 3, 2, 7, 6, 5) Odd elements: Queue(1, 3, 7, 5) Example #2: Scala // Scala program of filter() // method // Import Queue import scala.collection.mutable._ // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating queues val q1 = Queue(1, 3, 2, 7, 6, 5) // Print the queue println(q1) // Applying filter method val result = q1.filter(x => {x % 3 == 0}) // Displays output print("Elements divisible by 3: " + result) } } Output: Queue(1, 3, 2, 7, 6, 5) Elements divisible by 3: Queue(3, 6) Comment More infoAdvertise with us Next Article Scala Queue map() method with example R rupesh_rao Follow Improve Article Tags : Scala Scala Scala-Method scala-collection Similar Reads Scala Queue find() method with example The find() method is utilized to return an element that satisfy a given predicate in the queue. 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 progr 2 min read Scala Queue init() method with example The init() method is utilized to return a new queue that consists of all the elements except the last one. Method Definition: def init: Queue[A] Return Type: It returns a new queue that consists of all the elements except the last one. Example #1: Scala // Scala program of init() // method // Import 2 min read Scala Queue exists() method with example The exists() method is utilized to check whether a predicate holds for any of the elements of the queue. 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 queue or else returns false. Example #1: Scala 2 min read Scala Queue map() method with example The map() method is utilized to build a new queue by applying a function to all elements of this queue. Method Definition: def map[B](f: (A) => B): Queue[B] Return Type: It returns a new queue containing all the elements after applying the given function. Example #1: Scala // Scala program of map 2 min read 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 Map filter() method with example The filter() method is utilized to select all elements of the map which satisfies a stated predicate. Method Definition: def filter(p: ((A, B))=> Boolean): Map[A, B] Return Type: It returns a new map consisting all the elements of the map which satisfies the given predicate. Example #1: Scala // 1 min read Like