Scala ListMap

Immutable maps used a list-based data structure to implement. A linear, sequential list of things is stored in the Scala List class. ListMap requires the import of “scala.collection.mutable.ListMap”. Only a few elements are used with the ListMap collection. The key/value pairs make up a Scala map collection. Depending on its key, any value can be accessed. The Map requires unique values, but the keys are not required to be unique.

Due to the internal storage of entries, being in inverted insertion order, the most recent key is at the top of the list. Because of this, procedures like head and tail are O(n). Whereas the last and init are O(1). Only a limited number of elements may be accommodated by this collection because further actions such as adding or deleting entries are likewise O(n). In this guide, we will show you how to utilize the Scala’s Mutable ListMap in Ububtu 20.04 to carry out the routine tasks like initializing a ListMap, accessing entries by key, adding and removing elements, and creating an empty ListMap.

Syntax of ListMap

Our general representation of the ListMap in Scala is as follows:

var variable_name = ListMap("k1"->"v1"....)

Here, with the declaration of the variable name, we assigned the ListMap method. In the ListMap method, we have “k1” and “v1”. The “k1” indicates the key and the “v1” indicates the value.

Example 1: Using the ListMap Method for Creating a ListMap

The ListMap performs the operation in this instance. We can see that a ListMap is formed with values in the following code:

In the first step, we imported the library “scala.collection.immutable.ListMap” from the Scala packages as we can get the ListMap from this Scala library. Next, we established the object “example1” which has the main method definition inside its block. Now, we are in the main method where we can easily create the ListMap. Inside the variable “listMap”, we called the ListMap method. The ListMap contains three keys with its value and both have data type strings. After the creation of the list map, we printed them by passing the “listMap” variable in the println function.

The key/value pairs of the map are obtained from the ListMap method in the following screen:

Example 2: Using the ListMap Method for Adding and Accessing the Elements P

There is the creation of a ListMap as well as the addition of and access to elements. To comprehend the adding and accessing of the ListMap components in this context, let’s use the following example:

In the beginning, we inserted the Scala library “scala.collection.mutable.ListMap” which has the ListMap. Then, we created the object “example2”. Inside the main method of the “example2”, we declared a variable “listMap1” for the ListMap method. The ListMap has the keys for its value of string data type. We iterated each element in the ListMap by using the foreach loop. The foreach loop has the case statement for the iteration. Here, we accessed the value of key “3” by passing it in the “listMap1”. Then, we defined another variable “listMap2” for another ListMap method.

But this time, we used the ListMap method to add the key/value pairs in the already existing list map. We used the plus operator in between the “listMap1” and the new key/value pair. After that, with the foreach loop, we cycled over each element inside the listMap2.

Note that in the output, the first key/value pair generated is from the listMap1. The value “Three” is obtained from the specified key passing to the listMap1. Lastly, we have the appended list map from listMap2.

Example 3: Using the ListMap Method to Remove Elements

After creating a ListMap, an element is removed by using the minus symbol. This is an illustration on how to eliminate an element from a ListMap:

The object is defined as “example3” for this Scala script. Then, the main method definition is established. In the main() body, we declared a variable “listMap” for the ListMap method. The ListMap is assigned with the string keys and each key contains the string value. The foreach loop is applied to the variable “listMap” for the iteration over each element.

Next, we used the listMap variable but with the minus symbol and assigned the key “Age” to it. The minus symbol discarded the key “Age” along with its value from the ListMap. Then, we again iterated each element with the foreach loop after removing the operation from the ListMap.

First, the output obtained has all the elements that we inserted in the ListMap. Then, we have an output of the elements obtained after the minus operation on the ListMap.

Example 4: Using ListMap.Empty Method for Creating an Empty List Map

In this instance, we generate an empty ListMap. Either invoking the function constructor of the ListMap or using the ListMap.empty function produces an empty ListMap.

After the object “example4” creation, we have the main method definition. Then, we defined the variable “listMap1” which called the ListMap function with the new keyword. The new keyword is used here for the ListMap function constructor. By calling the ListMap constructor, we built the empty list map.

After that, there is another variable “listMap2” declaration. We assigned the ListMap.empty method to this variable to generate the empty list map. Both methods for creating the empty list map are printed through the println function.

Both methods provide the same results. The two empty list maps are created on the terminal as follows:


We learned how the ListMap function is utilized in Scala from the aforementioned article, as well as how to employ some examples of the ListMap that improve the efficiency of our programming. The ListMap function performs the action while using every value available and returns the result as a newer value without altering the original one. As a result, ListMap is a practical and optimal approach for structured programming in Scala with a range of data loading.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.