Scala list operations explained

61 total views, 1 views today

Welcome to Scala series. In this tutorial, you will learn about the Scala list and I have explained most of the list operations in detail.

  • List() is a collection to store values in an ordered fashion. It allows store duplicate values.
  • List() extends LinearSeq() trait and it is based on Last In First Out model.
  • List() is part of both immutable and mutable collection package of Scala. Immutable List allows us to apply a function on List but resulting in the creation of a new list every time. It doesn’t allow us the source. On the other hand, the mutable list allows modifying the source itself.

For more information on Scala immutable and mutable collection, please visit here.

Let’s get started in learning List operations and how to use them.

1. Create a List

var list:List[String] = List("a", "b", "c", "d")

In this example, we have created a list of four elements.

Create an empty List

var list:List[String] = List()

2. Get List Size

var list:List[String] = List("a", "b", "c", "d")
println(list.size)

Output  : 4

3. Access each element of a list

var list: List[String] = List("a", "b", "c", "d")
println(list(0))
println(list(1))
println(list(2))
println(list(3))

Result : 

a
b
c
d

4. Iterate the List

var list: List[String] = List("a", "b", "c", "d")
list.foreach(println _) //option 1
list.foreach(ele => println(ele)) //Option 2

Result

a
b
c
d

5. Get Head element

var list: List[String] = List("a", "b", "c", "d")
println(list.head)

Reuslt : a

6. Get Tail element

var list: List[String] = List("a", "b", "c", "d")
println(list.tail)

Reuslt : List(b, c, d)

7. Add an element in the list(Append in the rear)

var list: List[String] = List("a", "b", "c", "d")
var result = list.++("e")

result.foreach(println _)

Result
a
b
c
d
e

8. Add/merge a list to another list(Append in the rear)

var list: List[String] = List("a", "b", "c", "d")
var result = list.++(List("x", "y"))

result.foreach(println _)

Output :
a
b
c
d
x
y

9. Add an element in the list(Prepend in the front)

var list: List[String] = List("a", "b", "c", "d")
var result = list.::("e")

result.foreach(println _)

Result : 
e
a
b
c
d

10. Add/merge a list to another list(prepend in the front)

var list: List[String] = List("a", "b", "c", "d")
var result = list.:::(List("x", "y"))

result.foreach(println _)

Output:
x
y
a
b
c
d

11. Drop first N elements from the list(from the left to right)

var n: Int = 2
var list: List[String] = List("a", "b", "c", "d")
list.drop(n).foreach(println _)

Result:
c
d

12. Drop last N elements from the list(from the right to left)

var n: Int = 1
var list: List[String] = List("a", "b", "c", "d")
list.dropRight(n).foreach(println _)

Result:
a
b
c

13.Distinct elements

var list: List[String] = List("a", "b", "c", "d", "a", "c")
list.distinct.foreach(println _)

Result:
a
b
c
d

14. Find the min

var list: List[String] = List("a", "b", "c", "d")
println(list.min)

Result :  a

15. Find the max

var list: List[String] = List("a", "b", "c", "d")
println(list.max)

Result: d

16. Find the sum

var list: List[Int] = List(1, 2, 3, 4, 5)
println(list.sum)

Result : 15

17. Find the count

var list: List[String] = List("a", "b", "c", "d", "a", "c")
println(list.count(ele => ele.length() < 2))
  
Result : 6

18. Create a String from the list(mkstring)

var list: List[String] = List("a", "b", "c", "d", "a", "c") //Without seperator
println(list.mkString)

Result : abcdac

var list: List[String] = List("a", "b", "c", "d", "a", "c") //With seperator
println(list.mkString("-"))
  
Result : a-b-c-d-a-c

var list: List[String] = List("a", "b", "c", "d", "a", "c") //with start,seperator and end
println(list.mkString("/", "-", ";"))

Result : /a-b-c-d-a-c;

19. Reverse a list

var list: List[String] = List("a", "b", "c", "d", "a", "c")
println(list.reverse)

Output : List(c, a, d, c, b, a)

 

20. contains operation

var list: List[String] = List("a", "b", "c", "d", "a", "c")
println(list.contains("d"))

Output : true

 

21. filter operation

var list: List[String] = List("a", "b", "c", "d", "a", "c")
list.filter(ele => ele.equals("a")).foreach(println _)

Output : 

a
a

 

22. filterNot operation

var list: List[String] = List("a", "b", "c", "d", "a", "c")
list.filter(ele => ele.equals("a")).foreach(println _)

Output : 

b
c
d
c

 

23. find operation

var list: List[String] = List("a", "b", "c", "d", "a", "c")
var result = list.find(ele => ele.equals("d"))
println(result.get)

Output : d

 

24. Map operation

var list: List[String] = List("a", "b", "c", "d", "a", "c")
var result = list.map(ele => ele+"-"+ele)
result.foreach(println _)

Output:

a-a
b-b
c-c
d-d
a-a
c-c

 

25. flatMap operation

var list = List(List("a", "b", "c", "d", "a", "c"),List(1,2,3))
var result = list.map(ele => ele+"-"+ele)
result.foreach(println _)


Output

List(a, b, c, d, a, c)-List(a, b, c, d, a, c)
List(1, 2, 3)-List(1, 2, 3)

var list = List(List("a", "b", "c", "d", "a", "c"),List(1,2,3))
var result = list.flatMap(ele => ele)
result.foreach(println _)

Output

a
b
c
d
a
c
1
2
3

 

References: Scala List official documentation

I hope you like this tutorial and you were able to understand about Scala list and its various operations.

Thanks for reading and please give us a thumb up and comment below!

 

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.