×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Scala Lists

By IncludeHelp Last updated : October 20, 2024

What is a List in Scala?

List in Scala is a collection that stores data in the form of a liked-list. The list is an immutable collection which means the elements of a list cannot be altered after the list is created. Lists are flexible with data types, i.e. they can store elements of multiple data types.

Example

// this is a valid list in Scala
List(34, "Includehelp.com", 12.42)

Scala also gives its programmer to create an alterable list i.e. a mutable list using the ListBuffer class which is defined in the scala.collection.mutable package. There are a lot of utility methods defined for Lists to help the proper functioning of the List in Scala.

Defining a List

There are multiple syntaxes that can be used to define a list in Scala. Let's see each of them,

Method 1

val list_name: List[type] = List(element1, element2, element3...)

The list declared using this method will have all elements of the same data type.

Method 2

val list_name = List(element1, element2, element3...)

Some of the examples of valid lists:

  • Empty List,
    List()
    
  • List of values of same type,
    List(3, 65, 12, 1451, 99)
    
  • List of values of multiple types,
    List(1013, "Maths", 97.8)
    
  • 2-D list i.e ListofList,
    List(List(123, 41341), List(1, 46))
    

These were various types of List in Scala. Now, let's head to some action and create a list.

Example 1: Program to create a list of numbers

object MyClass {
    def main(args: Array[String]) {
        // creating List of Integer values 
        val number_list : List[Integer] = List(10, 25, 17, 93, 77)
        
        // printing List 
        println("The list of numbers is "+ number_list)
    }
}

Output:

The list of numbers is List(10, 25, 17, 93, 77)

Example 2: Program to create a 2-D mixed list

object MyClass {
    def main(args: Array[String]) {
        // creating 2D List  
        val list_2d = List(List(1, "C++", 1979), List(2, "JavaScript", 1995), List(3, "Scala", 2004))

        // printing List 
        println("The 2D list with dates of invension of programming languages is "+ list_2d)
    }
}

Output:

The 2D list with dates of invension of programming languages is List(List(1, C++, 1979), List(2, JavaScript, 1995), List(3, Scala, 2004))

After learning how to create a List in Scala, let's go through some operations on List.

Checking Empty List

The isEmpty method is used to check an empty list, it is an important function as prior to performing any of the complex operations on Scala, we need to make sure that the list has elements to perform operations on.

Syntax

list_name.isEmpty

This method returns a boolean value, true if the list is empty; false, otherwise.

Example

object MyClass {
    def main(args: Array[String]) {
        // creating  List  
        val list1 = List(12, 43, 99)
        val list2 = List()
        
        // printing List's 
        println("List1 : "+ list1)
        println("List2 : "+ list2)
        
        // check if the list isEmpty...
        println("list1.isEmpty = "+ list1.isEmpty)
        println("list2.isEmpty = "+ list2.isEmpty)        
    }
}

Output:

List1 : List(12, 43, 99)
List2 : List()
list1.isEmpty = false
list2.isEmpty = true

Getting First and Last Elements of List

The head method is used to het the first element of the List.

Syntax

List_name.head

This method returns a value, the first element of List.

The tail method is used to return all elements of the List except the first element. All the elements returned will be in the form of a list.

Syntax

List_name.tail

This method returns a list, all elements except the first.

Example

object MyClass {
    def main(args: Array[String]) {
        // creating  List  
        val nations = List("India", "America", "Japan", "Russia")

        //Printing the List 
        println("The List is : " + nations)

        //Printing head of the List
        println("List.head : " + nations.head)

        //Printing tail of the List
        println("List.tail : " + nations.tail)
    }
}

Output:

The List is : List(India, America, Japan, Russia)
List.head : India
List.tail : List(America, Japan, Russia)

Concatenating Lists

Concatenation is the process of merging to lists to one. You can append list (add a list at the end of another list) or prepend the list (adding a list at the start of another list).

The methods and operators to concatenate lists in Scala,

  1. ::: operator [appends the list]
  2. List.:::() method [prepends the list]
  3. List.concat() method [appends the list]

Concatenating lists using the ::: operator

You can append lists in Scala using the ::: operator.

list1 ::: list2 

It returns a list which is concatenated list of list1 and list2.

Example

Program to concatenate two lists of strings to one using ::: operator –

object MyClass {
    def main(args: Array[String]) {
        // creating  Lists  
        val bikes1 = List("RE ThunderBird 350" , "YRF R3")
        val bikes2 = List("HD Iron 883" , "BMW S1000RR")

        //Printing the Lists 
        println("List Bike1 : " + bikes1)
        println("List Bike2 : " + bikes2)

        //concatenating List 
        val conList = bikes1 ::: bikes2
        println("Concatenated List : " + conList)
    }
}

Output:

List Bike1 : List(RE ThunderBird 350, YRF R3)
List Bike2 : List(HD Iron 883, BMW S1000RR)
Concatenated List : List(RE ThunderBird 350, YRF R3, HD Iron 883, BMW S1000RR)

Concatenating lists using List .:::() method

You can prepend list in Scala using the List .:::() method.

list1.:::(list2)

It returns a list which is prepending list2 before list1.

Example

Program to concatenate two lists of numbers using List .:::() method –

object MyClass {
    def main(args: Array[String]) {
        // creating  Lists  
        val numList1 = List(4, 9, 21, 75)
        val numList2 = List(1, 7, 12, 25)

        //Printing the Lists 
        println("List numList1 : " + numList1)
        println("List numList2 : " + numList2)

        //concatenating List 
        val numList = numList1.:::(numList2)
        println("Concatenated List : " + numList)
    }
}

Output:

List numList1 : List(4, 9, 21, 75)
List numList2 : List(1, 7, 12, 25)
Concatenated List : List(1, 7, 12, 25, 4, 9, 21, 75)

Concatenating lists using List.concat() method

You can concatenate list in Scala using List.concat() method.

List.concat(list1, list2)

It returns the concatenated list.

Example

Program to concatenate two lists of type using the concat() method –

object MyClass {
    def main(args: Array[String]) {
        // creating  Lists  
        val numList = List(4, 9, 21, 75) // number list
        val stringList = List("includehelp" , "programming" , "tutorials") // String list

        //Printing the Lists 
        println("List numList : " + numList)
        println("List stringList : " + stringList)

        //concatenating List 
        val concatList = List.concat(numList, stringList)
        println("Concatenated List : " + concatList)
    }
}

Output:

List numList : List(4, 9, 21, 75)
List stringList : List(includehelp, programming, tutorials)
Concatenated List : List(4, 9, 21, 75, includehelp, programming, tutorials)

Creating list using fill() method

You can create a list of multiple copies of elements using the List.fill() method.

Syntax

List.fill(repeat_times)(element)

It returns a list with elements copied multiple times.

Example

Program to create a list by repeating string N times using fill() method –

object MyClass {
    def main(args: Array[String]) {
        // creating  List 
        val newList = List.fill(5)("includehelp")

        //Printing the Lists 
        println("newList : " + newList)
    }
}

Output:

newList : List(includehelp, includehelp, includehelp, includehelp, includehelp)

Reverse order of a list

You can reverse the order of a list in Scala using the reverse method.

Syntax

List.reverse

It returns a list with elements in reverse order.

Example

Program to reverse the given list of integers using reverse method –

object MyClass {
    def main(args: Array[String]) {
        // creating  Lists  
        val newList = 12 :: (54 :: (99 :: (1210 :: Nil)))

        //Printing the List 
        println("newList : " + newList)

        //Reversing List 
        val revList = newList.reverse

        // printing reverse list
        println("reversed List : " + revList)
    }
}

Output:

newList : List(12, 54, 99, 1210)
reversed List : List(1210, 99, 54, 12)

Tabulating list

You can create a list using a function that calculates for each value and adds the result to the list.

Syntax

List.tabulate(repeat_times)(evaluation_function)

It returns a list which has elements in tabular form.

Syntax

Program to create a list using tabulate() method –

object MyClass {
    def main(args: Array[String]) {
        // creating  Lists  
        val table5 = List.tabulate(10)(i => i*5)

        //Printing the List 
        println("table5 : " + table5)

        // creating a 2D List 
        val values = List.tabulate(5, 5)(_+_)

        //Printing the List
        println("values : " + values)
    }
}

Output:

table5 : List(0, 5, 10, 15, 20, 25, 30, 35, 40, 45)
values : List(List(0, 1, 2, 3, 4), List(1, 2, 3, 4, 5), List(2, 3, 4, 5, 6), List(3, 4, 5, 6, 7), List(4, 5, 6, 7, 8))

This is all about lists, creations and operations of List. There are some methods that are defined in the List class of Scala to support the functioning of List. You can see them at function reference of Scala List.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.