A list is a collection which contains immutable data. List represents linked list in Scala. The Scala List class holds a sequenced, linear list of items.
Following are the point of difference between lists and array in Scala:
- Lists are immutable whereas arrays are mutable in Scala.
- Lists represents a linked list whereas arrays are flat.
Syntax:
val variable_name: List[type] = List(item1, item2, item3)
or
val variable_name = List(item1, item2, item3)
Some important points about list in Scala:
- In a Scala list, each element must be of the same type.
- The implementation of lists uses mutable state internally during construction.
- In Scala, list is defined under scala.collection.immutable package.
- A List has various methods to add, prepend, max, min, etc. to enhance the usage of list.
Example:
import scala.collection.immutable._
object GFG
{
def main(args:Array[String])
{
val mylist1: List[String] = List("Geeks", "GFG",
"GeeksforGeeks", "Geek123")
val mylist2 = List("C", "C#", "Java", "Scala",
"PHP", "Ruby")
println("List 1:")
println(mylist1)
println("\nList 2:")
for(mylist<-mylist2)
{
println(mylist)
}
}
}
|
Output:
List 1:
List(Geeks, GFG, GeeksforGeeks, Geek123)
List 2:
C
C#
Java
Scala
PHP
Ruby
In above example simply we are printing two lists.
Example:
import scala.collection.immutable._
object GFG
{
def main(args:Array[String])
{
val emptylist: List[Nothing] = List()
println("The empty list is:")
println(emptylist)
}
}
|
Output:
The empty list is:
List()
Above example shows that the list is empty or not.
Example:
import scala.collection.immutable._
object GFG
{
def main(args:Array[String])
{
val twodlist: List[List[Int]] =
List(
List(1, 0, 0),
List(0, 1, 0),
List(0, 0, 1)
)
println("The two dimensional list is:")
println(twodlist)
}
}
|
Output:
The two dimensional list is:
List(List(1, 0, 0), List(0, 1, 0), List(0, 0, 1))
Basic Operations on Lists
The following are the three basic operations which can be performed on list in scala:
- head: The first element of a list returned by head method.
Syntax:
list.head //returns head of the list
Example:
import scala.collection.immutable._
object GFG
{
def main(args:Array[String])
{
val mylist = List("C", "C#", "Java", "Scala",
"PHP", "Ruby")
println("The head of the list is:")
println(mylist.head)
}
}
|
Output:
The head of the list is:
C
- tail: This method returns a list consisting of all elements except the first.
Syntax:
list.tail //returns a list consisting of all elements except the first
Example:
import scala.collection.immutable._
object GFG
{
def main(args:Array[String])
{
val mylist = List("C", "C#", "Java", "Scala",
"PHP", "Ruby")
println("The tail of the list is:")
println(mylist.tail)
}
}
|
Output:
The tail of the list is:
List(C#, Java, Scala, PHP, Ruby)
- isEmpty: This method returns true if the list is empty otherwise false.
Syntax:
list.isEmpty //returns true if the list is empty otherwise false.
Example:
import scala.collection.immutable._
object GFG
{
def main(args:Array[String])
{
val mylist = List("C", "C#", "Java", "Scala",
"PHP", "Ruby")
println("List is empty or not:")
println(mylist.isEmpty)
}
}
|
Output:
List is empty or not:
false
How to create a uniform list in Scala
Uniform List can be created in Scala using List.fill() method. List.fill() method creates a list and fills it with zero or more copies of an element.
Syntax:
List.fill() //used to create uniform list in Scala
Example:
import scala.collection.immutable._
object GFG
{
def main(args:Array[String])
{
val programminglanguage = List.fill(3)("Scala")
println( "Programming Language : " + programminglanguage )
val number= List.fill(8)(4)
println("number : " + number)
}
}
|
Output:
Programming Language : List(Scala, Scala, Scala)
number : List(4, 4, 4, 4, 4, 4, 4, 4)
Reversing List Order in Scala
The list order can be reversed in Scala using List.reverse method. List.reverse method can be used to reverse all elements of the list.
Syntax:
list.reverse //used to reverse list in Scala
Example:
import scala.collection.immutable._
object GFG
{
def main(args:Array[String])
{
val mylist = List(1, 2, 3, 4, 5)
println("Original list:" + mylist)
println("Reverse list:" + mylist.reverse)
}
}
|
Output:
Original list:List(1, 2, 3, 4, 5)
Reverse list:List(5, 4, 3, 2, 1)
Learn to code easily with our course
Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
14 Mar, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...