Scala Collections – List (sort, foreach)

Category : Scala | Sub Category : Scala Programs | By Prasad Bonam Last updated: 2020-10-08 13:39:02 Viewed : 546


Scala Collections – List (sort, foreach) 


·        override def apply[A](xs: A*): List[A]

 Creates a collection with the specified elements. 

·        final override def foreach[U](f: Int => U): Unit

Applies a function f to all elements of this list.

Note: this method underlies the implementation of most other bulk operations. Subclasses should re-implement this method if a more efficient implementation exists. 

·     def sorted[B >: Int](implicit ord: Ordering[B]): List[Int]

Sorts this list according to an Ordering.

The sort is stable. That is, elements that are equal (as determined by lt) appear in the same order in the sorted sequence as in the original. 

·        override def reverse: List[Int]

Returns new list with elements in reversed order. 

 Example:

Following example illustrates about Scala List-sort,foreach and merge

Save the file as −  ListExampleApp1.scala 

ListExampleApp1.scala

 package runnerdev 

object ListExampleApp1 extends App{ 

  //Applying predefined methods

  var list = List(125691012134)

  var list2 = List(100200)

  print("Elements: ") 

  // Iterating using foreach loop

 list.foreach((element: Int) => print(element + " ")) 

// Accessing element of 2 index

 print(" Element at 2 index: " + list(2))

  // Merging two list 

  var list3 = list ++ list2

  print(" Elements after merging list and list2: ")

  list3.foreach((element: Int) => print(element + " ")) 

  var list4 = list3.sorted // Sorting list

  print(" Elements after sorting list3: ")

  list4.foreach((element: Int) => print(element + " ")) 

  var list5 = list3.reverse // Reversing list elements

  print(" Elementss in reverse order of list5: ")

  list5.foreach((element: Int) => print(element + " "))

}

 

 

compile and run the above example as follows 

scala> scalac ListExampleApp1.scala

scala> scala ListExampleApp1 

Output:

Elements: 1 2 5 6 9 10 12 13 4

Element at 2 index: 5

Elements after merging list and list2: 1 2 5 6 9 10 12 13 4 100 200

Elements after sorting list3: 1 2 4 5 6 9 10 12 13 100 200

Elementss in reverse order of list5: 200 100 4 13 12 10 9 6 5 2 1


In Scala, you can use the List collection to perform various operations like sorting and iterating using foreach. Here is how you can do it:

1. Sorting a List:

You can sort a List in Scala using the sorted method. This method returns a new List with the elements sorted in ascending order by default. Here is an example:

scala
val myList = List(4, 2, 6, 1, 3, 5) // Sorting the list in ascending order val sortedListAsc = myList.sorted // Sorting the list in descending order val sortedListDesc = myList.sorted(Ordering[Int].reverse) println("Sorted List (Ascending): " + sortedListAsc) println("Sorted List (Descending): " + sortedListDesc)

Output:

mathematica
Sorted List (Ascending): List(1, 2, 3, 4, 5, 6) Sorted List (Descending): List(6, 5, 4, 3, 2, 1)

In this example, we first sort the myList in ascending order using the sorted method, and then we sort it in descending order by providing a custom ordering using Ordering[Int].reverse.

2. Iterating Over a List (Using foreach):

You can use the foreach method to iterate over the elements of a List and perform some operation on each element. Here is an example:

scala
val myList = List("apple", "banana", "cherry") // Iterate over the elements and print them myList.foreach { fruit => println(s"I like $fruit") }

Output:

css
I like apple I like banana I like cherry

In this example, we use foreach to iterate over each element in the list and print a message for each element.

These are two common operations you can perform on a Scala List collection: sorting and iterating using foreach. These operations make it easy to work with lists in Scala.

Search
Related Articles

Leave a Comment: