List

A list is collection which contains immutable  data and  is used to  store ordered elements.The elements of the list have same data type.It maintains order of elements and can contain duplicates elements also.

val fruits : List[String]= List("Apple","Orange","Greps","Papaya")
object MainObject {
def main(args: Array[String]) {
val fruits : List[String]= List("Apple","Orange","Greps","Papaya")
print(fruits)
}
}
Difference between Array and List
  • In Scala each element must be of the same type.
  • 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.
  • The type of a List which is of type T is represented as List[T]
String List 
val fruits = List("Apple","Orange","Greps","Papaya")
object MainObject {
def main(args: Array[String]) {
val fruits = List("Apple","Orange","Greps","Papaya")
print(fruits)
}
}
Integer List 
object MainObject {
def main(args: Array[String]) {
val num = List(1,2,3,4,5)
print(num)
}
}
List of Any type
object MainObject {
def main(args: Array[String]) {
val all = List(1,2,"Ayushi",true)
print(all)
}
}
Empty List 
Represents an empty List of anything of Zero Length.
val empty= Nil
object MainObject {
def main(args: Array[String]) {
val empty= Nil
print(empty)
}
}
Access the List Element
To print all elements of the List
object MainObject {
def main(args: Array[String]) {
val fruits = List("Apple","Orange","Greps","Papaya")
print(fruits)
}
}
To print the particular elements of the list
print(fruits(2)
object MainObject {
def main(args: Array[String]) {
val fruits = List("Apple","Orange","Greps","Papaya")
print(fruits(2))
}
}
Basic operation on List  
Head:- It returns the first element in the list
object MainObject {
def main(args: Array[String]) {
val number=List(1,2,3,4)
print(number.head)
}
}
res0: Int = 1
 
tail:- It returns all the except the first element in the list
object MainObject {
def main(args: Array[String]) {
val number=List(1,2,3,4)
print(number.tail)
}
}
res1: List[Int] = List(2, 3, 4)
 
last:- It returns the last element of list.
object MainObject {
def main(args: Array[String]) {
val number=List(1,2,3,4)
print(number.last)
}
}
res2: Int = 4
 
 isEmpty: its returns true value if the list is empty.
object MainObject {
def main(args: Array[String]) {
val number=List(1,2,3,4)
print(number.isEmpty)
}
}
res5: Boolean = false
 
 take(n): its returns first n elements of the list.
object MainObject {
def main(args: Array[String]) {
val number=List(1,2,3,4)
print(number.take(2))
}
}
res32: List[Int] = List(1, 2)

takeRight:- Return last N elements.
object MainObject {
def main(args: Array[String]) {
val number=List(1,2,3,4)
print(number.takeRight(2))
}
}
List(3,4)

Scala Collections has many methods which takes function as arguments and hence are considered to be Higher Ordered Function.

Reverse List Order
object MainObject {
def main(args: Array[String]) {
val country = List("Denmark","Sweden","France")
print(country.reverse)
}
}
res16: List[String] = List(France, Sweden, Denmark)

The List ‘range’ method
object test{
def main(args: Array[String])
{
var x = List.range(1,10)
println(x)
var y = List.range(0,10,2)
println(y)
var z = List.range(0,10,3)
println(z)
}
}
How to concatinate two List
object test {
def main(args: Array[String]) {
val a=List(1,2,3,4,5,6,7,8,9,10)
val b=List(11,12,13,14,15,16,17,18,19,20)
println("Elements from First list")
println(a)
println("Elements from Second list")
println(b)
println("Combine Elements from First and Second list")
println(a++b)
println("Combine Elements from First and Second list")
println(a:::b)
println("Combine Elements from First and Second list")
println(List.concat(a,b))
}
}
How to append element in the list
object test{
def main(args: Array[String])
{
val x = List(2)
println("Print the element of the List before")
println(x)
println("Print the element of the List After")
val y = 1 :: x
println(y)
println("prepend element to the List")
val z = 0 +: y
println(z)
}
}
Remove duplicate element form the List
object test {
def main(args: Array[String]) {
val fruits = List("Apple","Greps","Apple","Orange","Greps","Papaya","Papaya")
println("Distinct elemnts from the List")
println(fruits.distinct)
println("Distinct elemnts from the List")
println(fruits.toSet.toList)
}
}
How to identify Duplicates in List
object test {
def main(args: Array[String]) {
val list = List(1,1,1,2,3,4,5,5,6,100,101,101,102)
println(list)
println(list.size)
println(list.distinct.size)
println("return True Value if List Size equal")
println((list.distinct.size) == (list.size))
println("return differences")
println(list.diff(list.distinct))
println("return unique differences value")
println(list.diff(list.distinct).distinct)
println("return True Value if List Size is not equal")
println(list.toSet.size != list.size)
}
}
The List ‘fill’ method
You can also create a new List with the fill method:
val x = List.fill(3)("foo")
x: List[java.lang.String] = List(foo, foo, foo)

The List class ‘tabulate’ method
You can create a new List with the tabulate method of the List class. The tabulate method creates a new List whose elements are created according to the function you supply.
val x = List.tabulate(5)(n => n * n)
x: List[Int] = List(0, 1, 4, 9, 16)

Nested List
scala> val my_list = List(List(1,2,3),List(4,5,6))
my_list: List[List[Int]] = List(List(1, 2, 3), List(4, 5, 6))

Double and Int List
scala> val my_list = List(1,2,3.0)
my_list: List[Double] = List(1.0, 2.0, 3.0)

List of Tuple Pairs
scala> val my_list = List(("a",1),("b",2),("c",3))
my_list: List[(String, Int)] = List((a,1), (b,2), (c,3))

LISTBuffer :- It is used when an elements is needed to upended to end of the List

import scala.collection.mutable.ListBuffer
var fruits = new ListBuffer[String]()

// add one element at a time to the ListBuffer
fruits += "Apple"
fruits += "Banana"
fruits += "Orange"

// add multiple elements
fruits += ("Strawberry", "Kiwi", "Pineapple")

// remove one element
fruits -= "Apple"

// remove multiple elements
fruits -= ("Banana", "Orange")

object test
{
def main(args :Array[String])
{
  var a =List(1,2,4,9,7)

  if (a(3) == 9)
  {
  println("True")
  }
  else
  {
    println("False")
  }
}

val codes = List("abC", "Abc", "ABC", "xyz", "XyZ")
codes.map(_.toUpperCase())

Scala List Filter Method
object demo
{
def main(args:Array[String])
{
val m1 = List(5, 12, 3, 13)
val result = m1.filter(_ < 10)
println(result)
}
}
Difference between Array and List
 Array List
 An array is mutable in nature. A list is immutable in nature.
 An array fixed in size.
 List is Variable in size
 Array is flat.
 List represents a linked list.
 In Array we do random access.
  In List we can't do random access.
 In an array, memory is assigned during compile time. In a Linked list it is allocated during execution or runtime.

No comments:

Post a Comment