Scala Trait Traversable | Set-3 Last Updated : 15 Apr, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report Prerequisite : Scala Trait Traversable | Set-1 Scala Trait Traversable | Set-2 It is recommended to see (Set-1, Set-2) before this Set. The operations are as follows: Sub-Collection retrieval operations: The operations here are slice, drop, dropWhile, filter, filterNot, tail, take, takeWhile, and init. These operations are utilized to return some sub collection. Example : Scala // Scala program of Sub-Collection // retrieval operation // Creating object object SubCollection { // Main method def main(args: Array[String]) { // Creating List of numbers val x = List(17, 19, 21, 29, 31) // Applying Sub-Collection // retrieval operation val y = x.init // Displays all the elements of // the List except the last one println(y) } } Output: List(17, 19, 21, 29) Here, init retrieves all the elements of the Traversable collection except the last element. Example : Scala // Scala program of Sub-Collection // retrieval operation // Creating object object SubCollection { // Main method def main(args: Array[String]) { // Creating List of numbers val x = List(17, 19, 21, 29, 31) // Applying Sub-Collection // retrieval operation val y = x.tail // Displays all the elements of // the List except the first one println(y) } } Output: List(19, 21, 29, 31) Here, tail retrieves all the elements of the Traversable collection except the first element. Example : Scala // Scala program of Sub-Collection // retrieval operation // Creating object object SubCollection { // Main method def main(args: Array[String]) { // Creating List of numbers val x = List(27, 29, 31, 49, 51, 56) // Applying Sub-Collection // retrieval operation val y = x.slice(2, 5) // Displays the elements // from index 2 to 4 println(y) } } Output: List(31, 49, 51) Here, slice will return the elements from the given range of index excluding last index. Example : Scala // Scala program of Sub-Collection // retrieval operation // Creating object object SubCollection { // Main method def main(args: Array[String]) { // Creating List of numbers val x = List(37, 49, 51, 69, 71, 86) // Applying Sub-Collection // retrieval operation val y = x.take(2) // Displays the first two // elements of the list println(y) } } Output: List(37, 49) Here, this operation will return numbers of elements given in the argument of the take operation. Example : Scala // Scala program of Sub-Collection // retrieval operation // Creating object object SubCollection { // Main method def main(args: Array[String]) { // Creating List of numbers val x = List(33, 34, 36, 37, 39, 40, 44) // Applying Sub-Collection // retrieval operation val y = x.drop(4) // Displays all the elements of // the list except the first // four elements println(y) } } Output: List(39, 40, 44) Here, this operation will return all the elements of the collection except the first number of elements given in the argument of drop operation. Example: Scala // Scala program of Sub-Collection // retrieval operation // Creating object object SubCollection { // Main method def main(args: Array[String]) { // Creating List of numbers val x = List(2, 5, 9, 13, 17, 20) // Applying Sub-Collection // retrieval operation val y = x.dropWhile(_ < 10) // Displays all the elements of // the list which are greater // than or equal to 10 println(y) } } Output: List(13, 17, 20) Here, dropWhile will drop the elements until the given condition is satisfied and returns rest of the left elements. Example : Scala // Scala program of Sub-Collection // retrieval operation // Creating object object SubCollection { // Main method def main(args: Array[String]) { // Creating List of numbers val x = List(2, 5, 9, 10, 13, 17, 20) // Applying Sub-Collection // retrieval operation val y = x.takeWhile(_ < 13) // Displays all the elements of // the list which are less // than 13 println(y) } } Output: List(2, 5, 9, 10) Here, takeWhile will take the elements until the given condition is satisfied and returns these elements. Example : Scala // Scala program of Sub-Collection // retrieval operation // Creating object object SubCollection { // Main method def main(args: Array[String]) { // Creating List of numbers val x = List(9, 65, 99, 10, 23, 17, 12) // Applying Sub-Collection // retrieval operation val y = x.filter(_ < 23) // Displays all the elements of // the list which are less // than 23 println(y) } } Output: List(9, 10, 17, 12) Here, filter will return all the elements until the given condition is satisfied and drops the rest. Example : Scala // Scala program of Sub-Collection // retrieval operation // Creating object object SubCollection { // Main method def main(args: Array[String]) { // Creating List of numbers val x = List(9, 65, 99, 10, 23, 17, 12) // Applying Sub-Collection // retrieval operation val y = x.filterNot(_ < 23) // Displays all the elements of // the list which are greater // than or equal to 23 println(y) } } Output: List(65, 99, 23) Here, filterNot will return all the elements which does not satisfies the given condition and drops the rest. Subdivision operations: These operations include span, partition, splitAt, groupBy. These operations are utilized to break the elements of the collection and return some sub-collections. Example : Scala // Scala program of Subdivision // operations // Creating object object Subdivision { // Main method def main(args: Array[String]) { // Creating List of numbers val q = List(7, 9, 11, 15, 17, 19, 22) // Applying Subdivision // operations val r = q.span(_ < 15) // Displays all the elements in // two parts println(r) } } Output: (List(7, 9, 11), List(15, 17, 19, 22)) Here, span will split the given Traversable collection of element into two parts where, first part is obtained by performing takeWhile operation and second part is obtained by performing dropWhile operation. Example : Scala // Scala program of Subdivision // operations // Creating object object Subdivision { // Main method def main(args: Array[String]) { // Creating List of numbers val q = List(17, 29, 31, 36, 37, 39, 42) // Applying Subdivision // operations val r = q.partition(_ < 35) // Displays all the elements in // two parts println(r) } } Output: (List(17, 29, 31), List(36, 37, 39, 42)) Here, partition will divide the collection into two parts where, first part will return the elements till the given condition is satisfied and second part will return rest of the elements. Example : Scala // Scala program of Subdivision // operations // Creating object object Subdivision { // Main method def main(args: Array[String]) { // Creating List of numbers val q = List(17, 29, 31, 36, 37, 39, 42) // Applying Subdivision // operations val r = q.splitAt(4) // Displays all the elements in // two parts println(r) } } Output: (List(17, 29, 31, 36), List(37, 39, 42)) Here, splitAt will divide the elements of the collection into two parts in the given position where, first part will be obtained by performing take operation and second part will be obtained by performing drop operation. Example : Scala // Scala program of Subdivision // operations // Creating object object Subdivision { // Main method def main(args: Array[String]) { // Creating List of numbers val x = List(21, 20, 33, 40, 27, 62) // Applying Subdivision // operation val y = x.groupBy { // Partial function case z : Int if (z % 3 == 0) => z + 3 case z : Int if (z % 2 == 0) => z + 2 } // Displaying Map of lists println(y) } } Output: Map(42 -> List(40), 24 -> List(21), 64 -> List(62), 22 -> List(20), 36 -> List(33), 30 -> List(27)) Here, groupBy will divide the Traversable collection on the basis of the partial function given and will return a Map. Element test methods: These methods include forall, exists, and count. These methods are utilized to test the given collection of Traversable on the basis of stated conditions. Example : Scala // Scala program of Element // test // Creating object object Elementtest { // Main method def main(args: Array[String]) { // Creating List of numbers val x = List(21, 20, 33, 40, 27, 62) // Applying Element test // method val y = x forall (_ < 63) // Displays true if all the // elements are lesser // than 63 println(y) } } Output: true Here, forall will return true if all the elements of the collection satisfies the given condition else returns false. Example : Scala // Scala program of Element // test // Creating object object Elementtest { // Main method def main(args: Array[String]) { // Creating List of numbers val x = List(21, 20, 33, 40, 27, 62) // Applying Element test // method val y = x exists (_ < 30) // Displays true if some of // the elements are lesser // than 30 println(y) } } Output: true Here, exists will return true if some of the elements of the collection satisfies the given condition else returns false. Example : Scala // Scala program of Element // test // Creating object object Elementtest { // Main method def main(args: Array[String]) { // Creating List of numbers val x = List(21, 20, 33, 40, 27, 62) // Applying Element test // method val y = x count(_ < 40) // Displays the number of // elements satisfying the // given condition println(y) } } Output: 4 Here, count will display the number of elements of the collection which satisfies the given condition. Comment More infoAdvertise with us N nidhi1352singh Follow Improve Article Tags : Scala Scala scala-traits Similar Reads Scala Programming Language Scala is a general-purpose, high-level, multi-paradigm programming language. It is a pure object-oriented programming language which also provides support to the functional programming approach. Scala programs can convert to bytecodes and can run on the JVM (Java Virtual Machine). Scala stands for S 3 min read Scala Tutorial â Learn Scala with Step By Step Guide Scala is a general-purpose programming language that combines both object-oriented and functional programming. Designed for scalability and flexibility, it allows developers to write concise and maintainable code for everything from small scripts to large enterprise systems. First released in June 2 15+ min read Introduction to Scala Scala is a general-purpose, high-level, multi-paradigm programming language. It is a pure object-oriented programming language which also provides the support to the functional programming approach. There is no concept of primitive data as everything is an object in Scala. It is designed to express 7 min read Scala | flatMap Method In Scala, flatMap() method is identical to the map() method, but the only difference is that in flatMap the inner grouping of an item is removed and a sequence is generated. It can be defined as a blend of map method and flatten method. The output obtained by running the map method followed by the f 4 min read For Loop in Scala In Scala, for loop is also known as for-comprehensions. A for loop is a repetition control structure which allows us to write a loop that is executed a specific number of times. The loop enables us to perform n number of steps together in one line. Syntax: for(w <- range){ // Code.. } Here, w is 6 min read Scala vs Java Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented, etc. Java applications are compiled to bytecode that can run on any Java virtual machine (JVM) regardless of computer architecture. Scala is a general-purpose, high-level, multi-paradigm program 2 min read Hello World Program : First program while learning Programming In this article, I'll show you how to create your first Hello World computer program in various languages. Along with the program, comments are provided to help you better understand the terms and keywords used in theLearning program. Programming can be simplified as follows: Write the program in a 6 min read Scala Console | println, printf and readLine Console implements functions for displaying the stated values on the terminal i.e, with print, println, and printf we can post to the display. It is also utilized in reading values from the Console with the function from scala.io.StdIn. It is even helpful in constructing interactive programs. Let's 2 min read Class and Object in Scala Classes and Objects are basic concepts of Object Oriented Programming which revolve around the real-life entities. Class A class is a user-defined blueprint or prototype from which objects are created. Or in other words, a class combines the fields and methods(member function which defines actions) 5 min read Scala List filter() method with example The filter() method is utilized to select all elements of the list which satisfies a stated predicate. Method Definition: def filter(p: (A) => Boolean): List[A] Return Type: It returns a new list consisting all the elements of the list which satisfies the given predicate. Example #1: Scala // Sca 1 min read Like