Home » Scala language

Collection Hierarchy in Scala

Collection Hierarchy in Scala: The Scala collection has a rich hierarchy. At the root, there is Traversable trait and all the other collection class is its subclasses. In this tutorial, we will learn about the hierarchy of Scala collections.
Submitted by Shivang Yadav, on July 12, 2019

Scala Collection Hierarchy

Scala collections have a rich hierarchy. The traversable trait is at the root of Scala hierarchy, all classes inherit some traits that are required for the general functioning of the collections.

The below figure shows the collection hierarchy:

Collection Hierarchy in Scala

Traversable Trait

At the root, there is Traversable trait that implements a common method to all collections to traverse the collections.

Iterable trait

There is also a common trait for all collection classes used to iterate over the elements of a collection class by defining Iterator.

Inheriting common features for Traversable and iterable there are three main categories:

  1. Sequence (Seq)
  2. Sets(Set)
  3. Maps(Map)

1) Sequence

Sequence or Seq is a trait of iterable class that has defined the order of elements. This immutable trait provides indexing of elements in it. Some common seq methods are:

  • copyToarray() : copies elements of Seq to an array
  • indexOf() : finds the index of element passed
  • reverse() : return a new sequence with reverse order
  • inEmpty() : returns true if code is empty
  • indexWhere(element,from) : checks for index of element for the nth place
  • Max() : finds the maximum element of the sequence
  • Min() : finds the minimum element of the sequence

2) Sets

Scala Sets are a collection of different elements of the same type. There are no duplicate data elements in sets. Scala sets are mutable/ immutable based on how objects are changed? Sets are extended scala.collection.immutable or scala.collection.mutable package. Some common Sets methods are:

  • count() : counts the number of elements based on a condition
  • max[B :> A] : finds the largest element
  • min [B :> A] : finds the smallest element
  • Size : output the size of collection
  • toString() : creates a string representation on the object
  • copytoArray() : Creates an array with elements of the array

3) Maps

The map is a collection of key/value pairs. The value of an element is pointed by its key like an index but the key can be anything. Generally, maps in Scala are immutable type and its objects cannot be changed. But if required, the programmer can use scala.collection.mutable.map package to use the mutable version of it.

Methods that are valid for Maps trait are:

  • def iterator : Returns a new iterator over the map
  • clone() : makes a copy of the map
  • last() : returns the last element of map
  • get() : gets the value associated with the key
  • toSet() : Creates a set with all elements of the map
  • toString() : return a string with elements


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.