The Wayback Machine - https://web.archive.org/web/20240917200403/https://www.geeksforgeeks.org/scala-trait-traversable-set-3/
Open In App

Scala Trait Traversable | Set-3

Last Updated : 15 Apr, 2019
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 :




    
    

    Output:

    List(17, 19, 21, 29)
    

    Here, init retrieves all the elements of the Traversable collection except the last element.
    Example :




    
    

    Output:

    List(19, 21, 29, 31)
    

    Here, tail retrieves all the elements of the Traversable collection except the first element.
    Example :




    
    

    Output:

    List(31, 49, 51)
    

    Here, slice will return the elements from the given range of index excluding last index.
    Example :




    
    

    Output:

    List(37, 49)
    

    Here, this operation will return numbers of elements given in the argument of the take operation.
    Example :




    
    

    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:




    
    

    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 :




    
    

    Output:

    List(2, 5, 9, 10)
    

    Here, takeWhile will take the elements until the given condition is satisfied and returns these elements.
    Example :




    
    

    Output:

    List(9, 10, 17, 12)
    

    Here, filter will return all the elements until the given condition is satisfied and drops the rest.
    Example :




    
    

    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 :




    
    

    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 :




    
    

    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 :




    
    

    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 :




    
    

    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 :




    
    

    Output:

    true
    

    Here, forall will return true if all the elements of the collection satisfies the given condition else returns false.
    Example :




    
    

    Output:

    true
    

    Here, exists will return true if some of the elements of the collection satisfies the given condition else returns false.
    Example :




    
    

    Output:

    4
    

    Here, count will display the number of elements of the collection which satisfies the given condition.



Similar Reads

Scala Trait Traversable | Set-1
Introduction: A root trait of the entire class of the Scala collections is Trait Traversable. It is available at the uppermost position of the collection hierarchy. It has uniquely one abstract operation, which is foreach. Here each of the operations are assured to be executed in a single-threaded approach. Syntax: def foreach[U](f: Elem => U) H
4 min read
Scala Trait Traversable | Set-2
prerequisite- Scala Trait Traversable | Set-1In the previous Set we have seen some of the operations performed by the Class Taversable. Now, in this Set we will perceive some more operations. These operations are as follows: Conversion operations: The Conversion operations are toList, toSeq, toArray, toStream, toSet, toMap, toIterable, and toIndexe
8 min read
Scala Trait Traversable | Set-4
Prerequisite :- Scala Trait Traversable | Set-1 Scala Trait Traversable | Set-2 Scala Trait Traversable | Set-3 It is recommended to view Set-1, Set2 and Set-3 before this one. The operations are as follows: Folds: The operations here are reduceRight, reduceLeft, /: or foldLeft, :\ or foldRight. This methods apply binary operation to the successive
6 min read
Program to convert Java Set to a Traversable in Scala
A java Set can be converted to a Traversable collection in Scala by utilizing toTraversable method of Java in Scala. Here, we need to import Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Java set // to a Traversable
2 min read
Program to convert Java Set of strings to a Traversable in Scala
A java Set of strings can be converted to a Traversable collection in Scala by utilizing toTraversable method of Java in Scala. Here, we need to import Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Java set // to a T
2 min read
Program to convert Java Set of characters to a Traversable in Scala
A java Set of characters can be converted to a Traversable collection in Scala by utilizing toTraversable method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Java set // to
2 min read
Program to convert Java Set of floats to a Traversable in Scala
A java Set of floats can be converted to a Traversable collection in Scala by utilizing toTraversable method of Java in Scala. Here, we need to import Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Java set // to a Tr
1 min read
Program to convert Java Set of doubles to a Traversable in Scala
A java Set of doubles can be converted to a Traversable collection in Scala by utilizing toTraversable method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Java set // to a
1 min read
Program to convert Java Set of Shorts to a Traversable in Scala
A java Set of Shorts can be converted to a Traversable collection in Scala by utilizing toTraversable method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Java set // to a T
1 min read
Program to convert Java Set of bytes to a Traversable in Scala
A java Set of bytes can be converted to a Traversable collection in Scala by utilizing toTraversable method of Java in Scala. Here, we need to import Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Java set // to a Tra
1 min read
Program to convert Java list to Traversable in Scala
A java list can be converted to a Traversable collection in Scala by utilizing toTraversable method of Java in Scala. Here, we need to import Scala’s JavaConversions object in order to make this conversions work else an error will occur. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Jav
2 min read
Program to convert Java list of Strings to Traversable in Scala
A java list of Strings can be converted to a Traversable collection in Scala by utilizing toTraversable method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Java list // to
2 min read
Program to convert Java list of character to Traversable in Scala
A java list of characters can be converted to a Traversable collection in Scala by utilizing toTraversable method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Java list //
2 min read
Program to convert Java list of floats to Traversable in Scala
A java list of floats can be converted to a Traversable collection in Scala by utilizing toTraversable method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Java list // to a
1 min read
Program to convert Java list of doubles to Traversable in Scala
A java list of doubles can be converted to a Traversable collection in Scala by utilizing toTraversable method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Java list // to
1 min read
Program to convert Java list of Shorts to Traversable in Scala
A java list of Shorts can be converted to a Traversable collection in Scala by utilizing toTraversable method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Java list // to a
1 min read
Program to convert Java list of bytes to Traversable in Scala
A java list of bytes can be converted to a Traversable collection in Scala by utilizing toTraversable method of Java in Scala. Here, you need to import Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert Java list // to a
1 min read
Scala | Sealed Trait
Sealed provides exhaustive checking for our application. Exhaustive checking allows to check that all members of a sealed trait must be declared in the same file as of the source file. That means that all the possible known members of a trait that must be included are known by the compiler in advance. So this gives us advantage to prevent mistakes
4 min read
Scala | Trait App
App is a trait which is utilized to rapidly change objects into feasible programs, which is carried out by applying DelayedInit function and the objects inheriting the trait App uses this function to execute the entire body of the program as a section of an inherited main method. Note: trait App extends DelayedInit The Linear Super-types here are:
2 min read
Scala | Trait Mixins
We can extend several number of scala traits with a class or an abstract class that is known to be trait Mixins. It is worth knowing that only traits or blend of traits and class or blend of traits and abstract class can be extended by us. It is even compulsory here to maintain the sequence of trait Mixins or else the compiler will throw an error.
3 min read
Trait Linearization in Scala
Scala Linearization is a deterministic process which comes into play when an object of a class is created which is defined using inheritance of different traits and classes. linearization helps to resolve the diamond problem which occurs when a class or trait inherits a same property from 2 different concrete classes or traits. Syntax : trait C{} t
5 min read
Set in Scala | Set-1
A set is a collection which only contains unique items. The uniqueness of a set are defined by the == method of the type that set holds. If you try to add a duplicate item in the set, then set quietly discard your request. Syntax: // Immutable set val variable_name: Set[type] = Set(item1, item2, item3) or val variable_name = Set(item1, item2, item3
3 min read
Set in Scala | Set-2
Prerequisite: Set in Scala | Set-1Adding items in Mutable SetIn Set, We can only add new elements in mutable set. +=, ++== and add() method is used to add new elements when we are working with mutable set in mutable collection and += is used to add new elements when we are working with mutable set in immutable collection. Example 1: C/C++ Code // S
7 min read
Conversion from a Java Set to a Scala Set
A Java Set can be converted to a Scala Set by importing JavaConversions.asScalaSet method. Here, we need to call asScalaSet method which has a java Set as its argument. Therefore, this method returns a Scala Set. Now, lets see some examples. Example:1# // Scala program of converting a Java Set // to a Scala Set // Importing JavaConversions.asScalaS
2 min read
Program to print Java Set of characters in Scala
A java Set of characters can be returned from a Scala program by writing a user defined method of Java in Scala. Here, we don't even need to import any Scala’s JavaConversions object in order to make this conversions work. Now, lets see some examples. Example:1# // Scala program to print Java Set // of characters in Scala // Creating object object
2 min read
Scala Set &() method with example
The &() method is utilized to create a new set consisting of all elements that are present in both the given sets. Method Definition: Return Type: It returns a new set consisting of all elements that are present in both the given sets. Example #1: // Scala program of &() // method // Creating object object GfG { // Main method def main(args
1 min read
Program to convert Java set of Shorts to an Indexed Sequence in Scala
A java set of Shorts can be converted to an Indexed Sequence in Scala by utilizing toIndexedSeq method of Java in Scala. Here, we need to import Scala’s JavaConversions object in order to make this conversions work else an error will occur. Now, lets see some examples and then discuss how it works in details. Example:1# // Scala program to convert
2 min read
Scala Set dropRight() method with example
The dropRight() is utilized to return all elements except last 'n' elements. Method Definition: def dropRight(n: Int): Set[A] Return Type: It returns a set containing all elements except last 'n' elements. Example #1: // Scala program of dropRight() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating
1 min read
Scala Set equals() method with example
The equals() method is utilized to check whether the two sets have the same elements or not. Method Definition: def equals(that: Any): Boolean Return Type: It returns true if the elements of both the sets are same else it returns false. Example #1: // Scala program of equals() // method // Creating object object GfG { // Main method def main(args:A
1 min read
Scala Set drop() method with example
The drop() method is utilized to delete the first ‘n’ elements or to return all elements except first 'n' elements. Method Definition: def drop(n: Int): Set[A]] Return Type: It returns all elements except first 'n' elements. Example #1: // Scala program of drop() // method // Creating object object GfG { // Main method def main(args:Array[String])
1 min read
Article Tags :