Updated April 11, 2023
Introduction to Scala List Append
The following article provides an outline for Scala List Append. Scala list are immutable which means once assign object cannot change itself. List is also a part of collection which is used to store and retrieve elements. List in scala are some like array which store elements of same type only. Scala list internally uses linked list and as we know linked list are better suited for manipulation operation. To append or add any elements inside the list object directly we cannot do so because the list is immutable. So we can assign the elements while creating the object of the list.
Syntax:
We can append value to the list and listBuffer.
1. To append value inside the list object we have two approach shown below:
val variable_name = List(value1, value2, value3 , soon..)
val variable_name: List[data_type] = List(value)
2. To assign value for ListBuffer we use += operator to assign its value anytime because ListBuffer is mutable in nature.
var variable_name = new ListBuffer[data_type]()
variable_name += value1
variable_name += value1
variable_name += value1
How to Append List in Scala?
List is used to store elements. These elements have to be of same type only. In Scala list represent linked list data structure. In a singly linked list structure we have node which contained two things data and address to the next node. We can modify the values present inside scala list programmatically through our logic. Also list can contain duplicate elements in it and maintain the insertion order as well. Scala list is defined inside scala.collection.immutable package. We can have a look for its super type and sub classes.
As we know list is immutable so we can assign value or append value to the list once only for this we will create an object of list and assign its value.
Now we can have one practice syntax for this:
var list1 = List("a", "b", "c", "d", "e", "f")
In this way we can assign values to our list object without mentioning the data type. We just need to give the values inside the brackets. Another way we have is to mention the data type as well while creating the object.
vat list : List[Int] = List(100, 200, 300, 400, 500)
In this way we can assign or append value to our list object. But in this method we are also giving data type for our list variable. That means we cannot give another type of variable in it otherwise it will give us error.
In our case we are appending Int type values to the list so we are not allowed to add any other data type values like string, float etc.
Example:
Code:
import scala.collection.immutable._
object Main extends App{
// Your code here!
val list1: List[Int] = List(100, 200, 300, 400 , 500)
val list2 = List("initialize here", "hello1", "hello2", "hello3", "so on .....")
println("values inside first list are :: ")
println(list1)
println(list2)
}
One thing to keep in mind to work with list append is we have to import this package into our program import scala.collection.immutable._ Now we have created two list inside the main method one is integer list and second one is string type list. Also we have appended the value at the time of creation only and now we are just printing the element.
Examples of Scala List Append
Given below are the examples mentioned :
Example #1
In this example we are trying to add the elements inside the list and printing out the values after creation of list object.
Code:
import scala.collection.immutable._
object Main extends App{
// Your code here!
val list1: List[Int] = List(100, 200, 300, 400 , 500)
val list2 = List("initialize here", "hello1", "hello2", "hello3", "so on .....")
println("values inside first list are :: ")
println(list1)
// output
println("values inside second list are :: ")
for(value<-list2)
{
println("value is :: " +value)
}
}
Output:
Example #2
In this example we are creating a mutable list object. In mutable list object we are using += operator to append elements to our list object. To make it work we need to include import scala.collection.mutable.ListBuffer package into our program.
Code:
import scala.collection.mutable.ListBuffer
object Main extends App{
// Your code here!
// here we are creating the ListBuffer object
var list = ListBuffer[String]()
// assigning values
list += "Hello"
list += "byr"
list += "to all"
list += "to all 1"
list += "to all 2"
list += "to all 3"
println(list)
}
Output:
Example #3
In this example we are using append() method to add object inside list object it will also be a mutable object. To demonstrate this we have used ListBuffer because this is the way by which we can create a mutable list object in scala.
Code:
import scala.collection.mutable.ListBuffer
object Main extends App{
// Your code here!
// here we are creating the ListBuffer object
var list = ListBuffer[String]()
// assigning values
list += "Hello"
list += "byr"
list += "to all"
list += "to all 1"
list += "to all 2"
list += "to all 3"
println("values inside list before " + list)
// using method
list.append("new value append")
println("List after append method called :::: " + list)
}
Output:
Example #4
In this example we will use += operator to append values after the object creation.
Code:
import scala.collection.mutable.ListBuffer
object Main extends App{
// Your code here!
// here we are creating the ListBuffer object
var list = ListBuffer[String]()
// assigning values
list += "Hello"
list += "byr"
list += "to all"
list += "to all 1"
list += "to all 2"
list += "to all 3"
println("values inside list before " + list)
// using method
list += "new value append"
println("List after append operator called :::: " + list)
}
Output:
Conclusion
In scala we can append elements to the List object in one way only and that is while initializing of the object. This is because the List object is immutable in scala so we cannot change its value once we assign the object. But we have implementation of List by ListBuffer and so many classes. This class is mutable in nature so we have += operator to assign value into the listBuffer at any time.
Recommended Articles
We hope that this EDUCBA information on “Scala List Append” was beneficial to you. You can view EDUCBA’s recommended articles for more information.