Scala Iterator find() method with example
The find() method belong to the concrete value members of the class AbstractIterator. It is defined in the class IterableOnceOps. It finds the first element of the stated collection which satisfies the given predicate. It would not terminate for the infinite sized collections.
Method Definition : def find(p: (A) => Boolean): Option[A]
Return Type :It returns an Option value containing the first element of the stated collection that satisfies the used predicate else returns None if none exists.
Example #1:
// Scala program of find() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating an Iterator val iter = Iterator(2, 4, 5, 1, 13) // Applying find method val result= iter.find(x => {x % 2 == 0}) // Displays output println(result) } } |
Some(2)
Example #2:
// Scala program of find() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating an Iterator val iter = Iterator(3, 6, 15, 12, 21) // Applying find method val result= iter.find(x => {x % 3 != 0}) // Displays output println(result) } } |
None
Recommended Posts:
- Scala Iterator take() method with example
- Scala Iterator map() method with example
- Scala Iterator sum() method with example
- Scala Iterator next() method with example
- Scala Iterator zip() method with example
- Scala Iterator seq() method with example
- Scala Map iterator method with example
- Scala Iterator min() method with example
- Scala Iterator max() method with example
- Scala Iterator length() method with example
- Scala Iterator nonEmpty() method with example
- Scala Iterator size() method with example
- Scala Iterator hasNext() method with example
- Scala Iterator indexOf() method with example
- Scala Iterator dropWhile() method with example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



