Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
Program to convert Java Set of strings to a Traversable in Scala
Next article icon

Scala Trait Traversable | Set-2

Last Updated : 24 Jan, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

prerequisite- Scala Trait Traversable | Set-1
In 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 toIndexedSeq. These operations changes the Collection of Traversable into a relatively distinct thing. 
    Example : 
Scala
// Scala program of Conversion operation  // Creating object  object Conversion {      // Main method     def main(args: Array[String])      {          // Creating Set of numbers          val q = Set(2, 7, 8, 15, 19)          // Converting Set to an Array         val r = q.toArray           // Displaying an Array         println(r)     } } 

Output: 
[I@506e1b77

 
  • Here, the Conversion operation i.e, toArray will convert the above Set (or any Traversable) into an Array. 
    Example : 
Scala
// Scala program of Conversion operation  // Creating object  object Conversion {      // Main method     def main(args: Array[String])      {          // Creating Set of numbers          val q = Set(2, 6, 3, 7, 15, 20)          // Converting a Set to a List         val r = q.toList          // Displaying a List         println(r)              } } 

Output: 
List(20, 6, 2, 7, 3, 15)

 
  • Here, toList will convert any Collection of Traversable into a List. 
    Example : 
Scala
// Scala program of Conversion operation  // Creating object  object Conversion {      // Main method     def main(args: Array[String])      {          // Creating List of numbers          val x = List(9, 10, 13, 15, 18, 19)          // Converting a List to a Set         val y = x.toSet          // Displaying a Set         println(y)     } } 

Output: 
Set(10, 9, 13, 18, 19, 15)

 
  • Here, the Conversion operation i.e, toSet will convert any collection of Traversable into a Set. 
    Example : 
Scala
// Scala program of Conversion operation  // Creating object  object Conversion {      // Main method     def main(args: Array[String])      {          // Creating Set of numbers          val m = Set(2, 4, 6, 8, 11, 15)          // Converting a Set to a Sequence         val n = m.toSeq          // Displaying a Sequence         println(n)     } } 

Output: 
ArrayBuffer(6, 2, 11, 8, 4, 15)

 
  • Here, Conversion operation i.e, toSeq converts any Collection of Traversable into a Sequence. The sequence generated here is utilized in Vectors. 
    Example : 
Scala
// Scala program of Conversion operation  // Creating object  object Conversion {      // Main method     def main(args: Array[String])      {          // Creating Set of numbers          val x = Set(8, 10, 13, 15, 18)          // Converting a Set to an Iterable         val y = x.toIterable          // Displaying an iterable         println(y)     } } 

Output: 
Set(10, 13, 18, 8, 15)

 
  • Here, the Conversion operation i.e, toIterable (It Iterates over all the elements of the collection) will convert any collection of a Traversable into an Iterable. 
    Example : 
Scala
// Scala program of Conversion operation  // Creating object  object Conversion {      // Main method     def main(args: Array[String])      {          // Creating Set of numbers          val x = Set(2, 4, 5, 6, 7, 9)          // Converting a Set to an          // Indexed sequence          val y = x.toIndexedSeq          // Displaying an Indexed sequence         println(y)     } } 

Output: 
Vector(5, 6, 9, 2, 7, 4)

 
  • Here, the conversion operation i.e, toIndexedSeq converts any Traversable into an Indexed sequence. The Indexed sequence generated here is utilized in Strings and Vectors. 
    Example : 
Scala
// Scala program of Conversion operation  // Creating object  object Conversion {      // Main method     def main(args: Array[String])      {          // Creating Set of numbers          val x = Set(15, 17, 18, 19, 22, 25)          // Converting a Set to a stream          val y = x.toStream          // Displaying a Stream         println(y)     } } 

Output: 
Stream(25, ?)

 
  • Here, the Conversion operation i.e, toStream converts any collection of Traversable into a Stream. This Stream is enumerated lazily. 
    Example : 
Scala
// Scala program of Conversion operation  // Creating object  object Conversion {      // Main method     def main(args: Array[String])      {          // Creating a Set of parameters          val x = Set("GfG" -> "CS portal", "Nidhi" -> "a Geek")          // Converting a Set to a Map         val y = x.toMap          // Displaying a Map         println(y)     } } 

Output: 
Map(GfG -> CS portal, Nidhi -> a Geek)

 
  • Here, toMap will convert any Traversable to a Map. A Set or a List must have parameters. 
     
  • Size info operations: 
    The Size info operations are nonEmpty, isEmpty, hasDefiniteSize, and size. These operations can specify if the given operation is finite or infinite. 
    Example : 
Scala
// Scala program of Size info operation  // Creating object  object Sizeinfo {      // Main method     def main(args: Array[String])      {          // Creating a map          val x = Map("gfg" -> "cs", "nidhi" -> "geek")          // Applying Size info operation         val y = x.isEmpty          // Displays true if map is          // empty         println(y)     } } 

Output: 
false

 
  • Here, isEmpty checks if the Traversable collection is empty. If the collection of elements is empty then it prints true and if its not empty then it prints false. 
    Example : 
Scala
// Scala program of Size info operation  // Creating object  object Sizeinfo {      // Main method     def main(args: Array[String])      {          // Creating a map          val x = Map("gfg" -> "cs", "nidhi" -> "geek")          // Applying Size info operation         val y = x.nonEmpty          // Displays true if map is          // not empty         println(y)     } } 

Output: 
true

 
  • Here, nonEmpty checks if the Traversable collection contains elements. If there are elements in the collection then it displays true else false. 
    Example : 
Scala
// Scala program of Size info operation  // Creating object  object Sizeinfo {      // Main method     def main(args: Array[String])      {          // Creating a map          val q = Map("gfg" -> "cs", "nidhi" -> "geek",                                     "geeta" -> "coder")          // Applying Size info operation         val r = q.size          // Displays size of the Map         println(r)     } } 

Output: 
3

 
  • Here, size is utilized to evaluate the size of Traversable collection. 
    Example : 
Scala
// Scala program of Size info operation  // Creating object  object Sizeinfo {      // Main method     def main(args: Array[String])      {          // Creating a map          val q = Map("gfg" -> "cs", "nidhi" -> "geek",                                     "geeta" -> "coder")          // Applying Size info operation         val r = q.hasDefiniteSize          // Displays true if number of         // elements in Map are finite         println(r)     } } 

Output: 
true

 
  • Here, hasDefiniteSize is utilized to check if the Traversable collection has finite elements or not. If the collection is finite then it returns true else false. 
     
  • Element retrieval operations: 
    The Element retrieval operations includes last, head, lastOption, headOption, and find. These operations are utilized to retrieve first or last element of the Traversable collection or to retrieve the first element corresponding to the given condition. 
    Example : 
Scala
// Scala program of Element  // retrieval operation  // Creating object  object Retrieval {      // Main method     def main(args: Array[String])      {          // Creating a Set of numbers          val x = Set(12, 13, 14, 15)          // Applying element retrieval          // operation         val y = x.lastOption          // Displays last element         // of the Set         println(y)     } } 

Output: 
Some(15)

 
  • Here, last element of the Traversable is returned by lastOption. The stated collection must be ordered, if there are no elements in the collection then None is returned. 
    Example : 
Scala
// Scala program of Element  // retrieval operation  // Creating object  object Retrieval {      // Main method     def main(args: Array[String])      {          // Creating a Set of numbers          val x = Set(12, 13, 14, 15)          // Applying element retrieval          // operation         val y = x.last          // Displays last element         // of the Set         println(y)     } } 

Output: 
15

 
  • Here, last will return the last element of the stated collection. The collection must be ordered, if its not ordered then some random element is returned. 
    Example : 
Scala
// Scala program of Element  // retrieval operation  // Creating object  object Retrieval {      // Main method     def main(args: Array[String])      {          // Creating a List of numbers          val q = List(12, 24, 36, 48)          // Applying element retrieval          // operation         val r = q.head          // Displays first element         // of the List         println(r)     } } 

Output: 
12

 
  • Here, head will return first element of the Traversable collection if its ordered and if the collection is not ordered then any random element is returned. 
    Example: 
Scala
// Scala program of Element  // retrieval operation  // Creating object  object Retrieval {      // Main method     def main(args: Array[String])      {          // Creating a List of numbers          val x = List(8, 10, 21, 17, 29)          // Applying element retrieval          // operation          val y = x.find(_ % 3 == 0)           // Displays first element         // matching the condition         println(y)      } } 

Output: 
Some(21)

 
  • Here, find will retrieve the first element of the collection, which matches the stated condition. 
    Example : 
Scala
// Scala program of Element  // retrieval operation  // Creating object  object Retrieval {      // Main method     def main(args: Array[String])      {              // Creating List of numbers          val p = List(7, 9, 11, 19, 21)          // Creating a empty List          val q = List()          // Applying element retrieval          // operation          val r = p.headOption          val s = q.headOption           // Displays first element         // if the List is not empty         println(r)         println(s)      } } 

Output: 
Some(7) None

 
  • Here, headOption returns first element of an ordered collection but returns None if the collection is empty. 

Next Article
Program to convert Java Set of strings to a Traversable in Scala
author
nidhi1352singh
Improve
Article Tags :
  • Scala
  • Scala
  • scala-traits

Similar Reads

  • 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# // Scal
    2 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 i
    7 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
    2 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
    1 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, ite
    3 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. Exampl
    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 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 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 d
    2 min read
  • Scala BitSet ++:[B](that: TraversableOnce[B]) method with example
    Scala Bitsets are sets of non-negative integers which are represented as variable-size arrays of bits packed into 64-bit words. The ++:[B](that: TraversableOnce[B]) method is utilised create a collection containing the elements from the left hand operand followed by the elements from the right hand
    1 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences