EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Scala List

By Shalu SharmaShalu Sharma

Home » Software Development » Software Development Tutorials » Scala Tutorial » Scala List

Scala List

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

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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;

Popular Course in this category
Scala Programming Training (3 Courses,1Project)3 Online Courses | 9+ Hours | Verifiable Certificate of Completion | Lifetime Validity | 1 Project
4.5 (5,543 ratings)
Course Price

View Course

Related Courses
Programming Languages Training (41 Courses, 13+ Projects, 4 Quizzes)All in One Software Development Bundle (600+ Courses, 50+ projects)
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:

Scala List-1.1

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:

Scala List-1.2

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:

Scala List-1.3

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:

Scala List-1.4

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:

Output-1.5

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:

Output-1.6

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:

Output-1.7

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:

Output-1.8

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

This is a guide to Scala List. Here we also discuss the definition and how does the list work in scala? along with different examples and its code implementation. You may also have a look at the following articles to learn more –

  1. Scala for Loop
  2. Scala While Loops 
  3. Scala Collections
  4. Scala foreach

Scala Programming Training (3 Courses)

3 Online Courses

9+ Hours

Verifiable Certificate of Completion

Lifetime Validity

1 Project

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Scala Tutorial
  • Advanced
    • Constructors in Scala
    • Scala Class Constructor
    • Scala Abstract Class
    • Scala String
    • Scala Collections
    • Scala High Order Functions
    • Scala Date Function
    • Scala Stream
    • Scala Class
    • Scala Function
    • Scala Closure
    • Scala Range
    • Scala Either
    • Scala Random
    • Scala Tuples
    • Scala Partial Function
    • Scala Split
    • Scala Finds
    • Scala List
    • Scala Trait
    • Scala Try Catch
    • Scala Regex
    • Scala Vector
    • Scala Seq
    • Scala Byte
    • Scala ListBuffer
    • Scala ArrayBuffer
    • Scala flatMap
    • Scala HashMap
    • Scala Map Function
    • Scala SortBy
    • Scala Some
    • Scala Generic
    • Scala getOrElse
    • Scala REPL
    • Scala Logging
    • Scala Lambda
    • Scala Queue
    • Scala Set
    • Scala Singleton
    • Scala groupBy
    • Scala reduce
    • Scala filter
    • Scala Option
    • Scala Native
    • Scala DataFrame
    • Scala Anonymous Function
    • Scala Read File
    • Scala Write to File
    • Scala Pattern Matching
    • Scala ClassTag
    • Scala Synchronized
    • Scala zipwithindex
    • Scala Interview Questions
  • Basics
    • What is Scala?
    • Uses of Scala
    • Scala Versions
    • Scala Data Types
    • Scala Companion Object
    • Scala Operators
    • Scala: The Functional Approach
    • Scala Object
    • Scala Implicit
    • Scala IDE
  • Control Flow
    • Scala if else
    • Break in Scala
    • Scala for Loop
    • Scala foreach
    • Scala While Loops

Related Courses

Scala Programming Training Course

Programming Language Course

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Scala Programming Training (3 Courses) Learn More