Updated April 14, 2023
Definition of Scala List
Scala list is immutable which means once assign object cannot change itself. List is also a part of the collection that is used to store and retrieve elements. List in scala is some like array which store elements of the same type only. Scala list internally uses a linked list and as we know the linked list is better suited for manipulation operation. It is based on linked List data structure and immutable in nature which means object once assign cannot change it, they maintain the insertion order as well and come up with a various method which enhance working with a list, they are the part of the collection in Scala only.
Syntax of Scala List
In Scala, we can create a list in two ways
We can assign value to the list while creating the list object without defining the data type. We can also create a list with data type declaration with it so only that type of data we can assign to a specific list later.
valvariable_name: List[data_type] = List(element1, element2 element3, element4)
and
valvariable_name = List(element1, element2 element3, element4)
In the above syntax, we have two ways to declare or create the list object.
Example:
val list1: List[Int] = List(100, 200, 300, 400, 500)
and
val list2 = List("hello", "hello 2", "hello3 ", "so on..")
How does List Work in Scala?
List is used to store elements. These elements has to be of the same type only. In Scala list represent the linked list data structure. We can discuss the linked list of how it works;
In a singly linked list structure, we have a node which contained two things data and address to the next node see in below example. we can modify the values present inside the Scala list programmatically through our logic. Also, the list can contain duplicate elements in it and maintain the insertion order as well. It is defined inside scala.collection.immutable package. We can have a look for its supertype and subclasses.
Superclasses ;
DefaultSerializable | StrictOptimizSeqOps | LinearSeq | LinearSeqOps | AbstractSeq |
Iterable | AbstractIterable | Seq | SeqOps | PartialFunction |
IterableactoryDefaults | IterableOps | IterableOnceOps | AnyRef | Any |
The Scala list offers various methods. Few are described below;
Method Name | Return Type |
sortBy | List |
sortWith | List |
Sum | Sum of all values in the list |
++ | List |
+ | List |
Scala list hierarchy;
- AbstarctSeq
- LinearSeq
- LinerSeqOps
- StrictOptimizedLinearSeqOps
- StrictOptimizedSeqOps
- IterableFactoryDefaults
- UnliftOps
We should keep in mind the below points while working with Scala list;
- It is immutable and it is defined under scala.collection.immutable package.
- The Scala list is based on a linked list data structure.
- It provides us various methods to deal with the list.
- Data present insideScala list should be of the same type only.
Method and Examples of Scala List
Following are the list of methods and examples as given below:
1. for()
This method is just we are using to print the list element of different types. see below;
Example
import scala.collection.immutable._
object Main extends App{
// Your code here!
val list1: List[Int] = List(100, 200, 300, 400 , 500)
val list2 = List("initilize 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:
2. ++
This method return a new list that contains all the elements from both the list.
Example
import scala.collection.immutable._
object Main extends App{
// Your code here!
val list1: List[Int] = List(100, 200, 300, 400, 500)
val list2: List[Int] = List(800, 900, 1500, 1700, 300)
println("values inside first list are :: ")
println(list1)
println("values inside second list are :: ")
println(list2)
// applying function
val result = list1 ++ list2
println("result is ::: " + result)
}
Output:
3. head
This method will return the first element of the list.
Example
import scala.collection.immutable._
object Main extends App{
// Your code here!
val list1: List[Int] = List(100, 200, 300, 400, 500)
val list2: List[Int] = List(800, 900, 1500, 1700, 300)
println("values inside first list are :: ")
println(list1)
println("values inside second list are :: ")
println(list2)
// applying function
val result = list1.head
println("result is ::: " + result)
}
Output:
4. tail
This method returns a list which contains all element but not the first element.
Example
import scala.collection.immutable._
object Main extends App{
// Your code here!
val list1: List[Int] = List(100, 200, 300, 400, 500)
val list2: List[Int] = List(800, 900, 1500, 1700, 300)
println("values inside first list are :: ")
println(list1)
println("values inside second list are :: ")
println(list2)
// applying function
val result = list1.tail
println("result is ::: " + result)
}
Output:
5. Empty
This method check list is empty or not and returns true or false based on the result.
Example
import scala.collection.immutable._
object Main extends App{
// Your code here
val list1: List[Int] = List(100, 200, 300, 400, 500)
val list2: List[Int] = List(800, 900, 1500, 1700, 300)
println("values inside first list are :: ")
println(list1)
println("values inside second list are :: ")
println(list2)
// applying function
val result = list1.empty
println("result is ::: " + result)
}
Output:
6. Concat
This method is used to concate two list and returns a new list.
Example
import scala.collection.immutable._
object Main extends App{
// Your code here!
val list1: List[Int] = List(100, 200, 300, 400, 500)
val list2: List[Int] = List(800, 900, 1500, 1700, 300)
println("values inside first list are :: ")
println(list1)
println("values inside second list are :: ")
println(list2)
// applying function
val result = List.concat(list1 , list2)
println("result is ::: " + result)
}
Output:
7. reverse
This will reverse the list element and print them from the end.
Example
import scala.collection.immutable._
object Main extends App{
// Your code here!
val list1: List[Int] = List(100, 200, 300, 400, 500)
val list2: List[Int] = List(800, 900, 1500, 1700, 300)
println("values inside first list are :: ")
println(list1)
println("values inside second list are :: ")
println(list2)
// applying function
val result = list1.reverse
println("result is ::: " + result)
}
Output:
8. indexOf()
This method returns the index of elements.
Example
import scala.collection.immutable._
object Main extends App{
// Your code here!
val list1: List[Int] = List(100, 200, 300, 400, 500)
val list2: List[Int] = List(800, 900, 1500, 1700, 300)
println("values inside first list are :: ")
println(list1)
// applying function
val result = list1.indexOf(300)
println("result is ::: " + result)
}
Output:
Conclusion
It based on the linked List data structure. Where the array is flat. It is immutable also. List is also used to store and retrieving of the element. It maintains the insertion order that means in which order we have added can be obtained in the same sequence.
Recommended Articles
We hope that this EDUCBA information on “Scala List” was beneficial to you. You can view EDUCBA’s recommended articles for more information.