Scala Lists
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:
// Scala program to print immutable lists import scala.collection.immutable. _ // Creating object object GFG { // Main method def main(args : Array[String]) { // Creating and initializing immutable lists val mylist 1 : List[String] = List( "Geeks" , "GFG" , "GeeksforGeeks" , "Geek123" ) val mylist 2 = List( "C" , "C#" , "Java" , "Scala" , "PHP" , "Ruby" ) // Display the value of mylist1 println( "List 1:" ) println(mylist 1 ) // Display the value of mylist2 using for loop println( "\nList 2:" ) for (mylist < -mylist 2 ) { println(mylist) } } } |
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:
// Scala program to illustrate the // use of empty list import scala.collection.immutable. _ // Creating object object GFG { // Main method def main(args : Array[String]) { // Creating an Empty List. val emptylist : List[Nothing] = List() println( "The empty list is:" ) println(emptylist) } } |
The empty list is: List()
Above example shows that the list is empty or not.
Example:
// Scala program to illustrate the // use of two dimensional list import scala.collection.immutable. _ // Creating object object GFG { // Main method def main(args : Array[String]) { // Creating a two-dimensional List. 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) } } |
The two dimensional list is: List(List(1, 0, 0), List(0, 1, 0), List(0, 0, 1))
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:
// Scala program of a list to
// perform head operation
import
scala.collection.immutable.
_
// Creating object
object
GFG
{
// Main method
def
main(args
:
Array[String])
{
// Creating a List.
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:
// Scala program to perform
// tail operation of a list
import
scala.collection.immutable.
_
// Creating object
object
GFG
{
// Main method
def
main(args
:
Array[String])
{
// Creating a List.
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:
// Scala program to perform
// isEmpty operation of a list
import
scala.collection.immutable.
_
// Creating object
object
GFG
{
// Main method
def
main(args
:
Array[String])
{
// Creating a List.
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
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:
// Scala program to creating a uniform list import scala.collection.immutable. _ // Creating object object GFG { // Main method def main(args : Array[String]) { // Repeats Scala three times. val programminglanguage = List.fill( 3 )( "Scala" ) println( "Programming Language : " + programminglanguage ) // Repeats 2, 10 times. val number = List.fill( 8 )( 4 ) println( "number : " + number) } } |
Programming Language : List(Scala, Scala, Scala) number : List(4, 4, 4, 4, 4, 4, 4, 4)
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:
// Scala program of reversing a list order import scala.collection.immutable. _ // Creating object object GFG { // Main method def main(args : Array[String]) { val mylist = List( 1 , 2 , 3 , 4 , 5 ) println( "Original list:" + mylist) // reversing a list println( "Reverse list:" + mylist.reverse) } } |
Original list:List(1, 2, 3, 4, 5) Reverse list:List(5, 4, 3, 2, 1)
Please Login to comment...