Scala is becoming a high-level programming language as it is concise object-oriented and has different functional programming languages. A critical distinct about these functional languages from a structure base is that we cannot add, modify, or alter any change in them as functions are immutable, specific to a Scala list. This section of the article will introduce and demonstrate the most common ways and methods to append in the Scala list.

What is Append to List in Scala?

Scala list is a class in scala that is a commonly used data structure in scala applications that holds the sequence list of elements of the same type. As scala lists are immutable too, that means we cannot modify or append elements value of the list once it is assigned, and secondly, lists represent the link list that is convenient for manipulation of operations.

How Scala Appends to List?

We can append values to the list and list buffer. Let’s get started to append in Scala list and ListBuffer from the basics of using the common methods in Scala. You should take a look at the following examples. All the executions are in Ubuntu 20.04

Example # 1: Using ‘: ’ method to Append Scala List:

This is the first method we use to append Scala List using the operator “: ”. The syntax we use in this method is; first to declare the list name and then use the ‘: ’ method rather than the new element that will be appended in the list. The syntax looks like “List name: new elements”. The program is as follows:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Scala-Append-to-list-1.jpg" data-lazy- height="287" src="data:image/svg xml,” width=”732″>

In the above example, we have declared a Scala List class as a “family” and defined our main() function. In our main function, we have supplied command-line arguments as an array of strings. With the variable value name “xlist” we have initialized our list string with three different names. Note that we are only using one data type in the whole program: a string. No other data type is declared in the program. As we have initialized our string list, now we are printing the values of the list string “xlist” that will show the present list. After we have a variable value with the keyword “ylist” that will append the present list and the new string value by using the “: ” method like ylist: “are brothers”.

On executing this Scala script, we have a modified string list that carries the previous list string elements and the new list elements together. You can see the new list string elements in the image below.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Scala-Append-to-list-2.jpg" data-lazy- height="171" src="data:image/svg xml,” width=”716″>

Example # 2: Using the “:::” method to Append Scala List:

In this example, we use another method to append the Scala list. The method uses the “:::” sign to append or concatenate list strings. When we have to append multiple values in a list, this method will be beneficial. The program of how this “:::” operator will work is as follows.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Scala-Append-to-list-3.jpg" data-lazy- height="340" src="data:image/svg xml,” width=”805″>

We have declared a Scala List class that is represented as “calculate” in this particular program. Then we have defined our main() function with an argument as arrays of string. Remember that we have to use only one data type while initializing the Scala list string or an array. Here we have a variable value with the name “a” defined that initializes the list string having a string “Multiple values of 5” then we have another variable value represented with the keyword “b” that stores another Scala list string. In this “b” variable, note that we are passing string numbers value.

At last, we have a variable value with the name “c” that will append the two strings “a” and “b” with the “:::” operator method. Side by side, we are printing the Scala list strings to show you how they were before and how they will be after appending the method. We are printing the Scala list strings with printing statements.

After implementing and interpreting the above program, we have an appended list string shown on the below screen.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Scala-Append-to-list-4.jpg" data-lazy- height="149" src="data:image/svg xml,” width=”728″>

Example # 3: Using “= ” and append() method to Append Scala Array:

In our last example, we have the “ =” and append() method to append the Scala list string elements. Here we have a mutable data structure such as ListBuffer. To append listBuffer in our program, we have to define Scala list inside the “Scala.collection.mutable.Listbuffer” package. Let’s go ahead with the program below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Scala-Append-to-list-5.jpg" data-lazy- height="441" src="data:image/svg xml,” width=”822″>

In this example, we have used ListBuffer because we can create a mutable Scala list object only in this way. At first, we have imported “Scala.collection.mutable.ListBuffer” to access the mutable ListBuffer Structure, then we have declared a Scala list class with the name “hello”. In that class, we have our main() function taking array strings as a command-line argument; then, we define a variable with the keyword “currentlist”. This currentlist has a ListBuffer string that stores different elements with the operator “ =”. We have to append elements in the Scala list string with this method. Then we have another string element that we are calling into another method, currentlist.append(). So that it will append the current list elements with the elements we want to append. Lastly, we have a printing value from this append() method.

We have the output of the above execution Scala program that shows the modified ListBuffer string. We have successfully appended the ListBuffer string from the “ =” and append() method. You can see the ListBuffer string on the Ubuntu terminal screen.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/Scala-Append-to-list-6.jpg" data-lazy- height="122" src="data:image/svg xml,” width=”729″>

Conclusion:

In summation of this Scala append to list article, we have gone through simple and easy-to-use methods with running examples. While initializing the object, we can append it to Scala list objects. As Scala objects are immutable, we cannot modify or change them once we assign them a value. But as we thoroughly discussed the implementation of various methods with them, we can assign a value in Scala list and ListBuffer. We have tried to show each example from these fundamental Scala list methods.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/IMG-20211027-WA0029-150×150.jpg6220578d76f19.jpg" height="112" src="data:image/svg xml,” width=”112″>

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.