What is Data Structure
We are here to learn basic about Data Structure in Scala
A Data Structure in scala is a type of data structure that aids in the storage, organisation, and retrieval of data in the field of computing in general. It is important to note that Scala distinguishes between immutable and mutable collection data types.
Mutable collections are organise under the scala.collection.mutable package, while immutable collections are organise under the scala.collection.immutable package.
As its a Basic introduction of Data Structure in Scala, it’s a good idea to go over some common data structures.These can be broadly classified as an array, a map, a list, a tree, a set, and a queue.
An Array Data Structure
The array, which stores a fixed-size sequence of elements of the same type. A collection of data is store in an array, but it is often more useful to think of an array as a collection of variables of the same type.
Declaring Array Variables
To use an array in a programme, you must declare a variable to refer to the array and specify the type of array that the variable can refer to.
var z = Array[String] = new Array[String](3) or var z = new Array[String](3)
In this case, z is declare as a String array with a maximum of three elements. Values can be assigned to individual elements, or access to individual elements can be gained, by using commands such as the ones listed below.
z(0) = "Zara"; z(1) = "Nuha"; z(4/2) = "Ayan"
whereas the final example demonstrates that, in general, the index can be any expression that yields a whole number. There is yet another way to define an array.
var z = Array("Zara", "Nuha", "Ayan")
The image below depicts an array called myList. MyList contains ten double values with indices ranging from 0 to 9.
Below is an example program of showing how to create, initialize and process arrays −
A List Data Structure
A list is a collection of unchangeable data. In Scala, a list represents a linked list. The Scala List class represents a sequentially ordered, linear list of items.
The following are the differences between lists and arrays in Scala:
In Scala, lists are immutable, whereas arrays are mutable.
And lists are linked lists, whereas arrays are flat.
val variable_name: List[type] = List(item1, item2, item3)
val variable_name = List(item1, item2, item3)
Some key points about lists in Scala:
Each element in a Scala list must be of the same type.
Internally, the implementation of lists makes use of mutable state during construction.
List is definedin Scala by the scala.collection.immutable package.
A List has various methods to add, prepend, max, min, and so on to improve its usage.
Basic List Operations
All operations on lists can be express using one of the three methods listed below.
Scala Map is a set of key/value pairs. Any value can be retrieve based on its key. The keys are unique in the map, but the values don’t need to be unique. Cards are also known as hash tables. There are two types of tags, immutable and mutable. The difference between mutable and immutable objects is that when an object is immutable, the object itself cannot be change.
By default, Scala uses immutable maps. If you want to use mutable maps, you’ll need to explicitly import the scala.collection.mutable.Map class. If you want to use both mutable and immutable maps, you can still refer to immutable maps as a map, but you can refer to mutable sets as mutable maps.
Basic Operation on MAP
A Set Data Structure
A set is a special data structure that reinforces the uniqueness of data points. Typically, you would use an aggregation to store reference data, such as a list of user connections or user IDs that cannot be duplicated. The use of sets is explicitly derive in mathematics252 to facilitate operations such as association, intersection, and set difference.
A Queue Data Structure
A queue is a first-in, first-out (FIFO) data structure. Scala provides both an immutable queue and an editable queue. The editable queue can be update or expand in place. This means that one can modify, add or remove items from the queue as a side effect whereas the queue is immutable which never changes. In Scala, a queue is implemented as a pair of lists. One is used to insert items and the second to contain deleted items. Items are added to the first list and removed from the second list. The two most basic queue operations are Enqueue and Dequeue.
Enqueue – Adds an item to the end of the queue.
Dequeue – Removes an item from the top of the queue.
Methods in Queue:
- +=: This method allows you to add an element to the end of the queue.
- ++=: This method is used to insert more than one item at the end of the queue.
- clear: remove all items from the queue.
- dequeue: returns the first element of the queue
- enqueue: add all items to the queue.
- equals: checks if two queues are structurally similar.
- front: returns the first item in the queue.
- isEmpty: check if the queue is empty.
A tree is a hierarchical data structure that stores entries at a root node and branches into subsequent child nodes. The use of trees is particularly useful in indexing and search applications, as well as in data science for predictive analytics.