An Overview of the Collections API :
1) For instance, every kind of collection can be created by the same uniform syntax, writing the collection class name followed by its elements.
Traversable(1, 2, 3)
Iterable("x", "y", "z")
Map("x" -> 24, "y" -> 25, "z" -> 26)
Set(Color.red, Color.green, Color.blue)
SortedSet("hello", "world")
Buffer("x", "y", "z")
IndexedSeq(1.0, 2.0)
LinearSeq("a", "b", "c")
2) The same principle also applies for specific collections like HasMap, List … etc
List(1, 2, 3)
HashMap("x" -> 24, "y" -> 25, "z" -> 26)
3) All these collections get displayed with toString in the same way they are written above.
CollectionAPIExample1.scala
import java.awt.Color
import scala.collection.mutable.LinearSeq
import scala.collection.mutable.Buffer
import scala.collection.mutable.SortedSet
import scala.collection.mutable.LinearSeq
object CollectionAPIExample1 {
def main(args: Array[String]): Unit = {
println("Collection API First Example")
println("===============================================================================")
println("Traversable object values are: " + Traversable(1, 2, 3).toString() )
println("nIterable object values are: " + Iterable("x", "y", "z").toString() )
println("nMap object values are: " + Map("x" -> 24, "y" -> 25, "z" -> 26).toString() )
println("nSet object values are: " + Set(Color.red, Color.green, Color.blue).toString())
println("nSortedSet object values are: " + SortedSet("hello", "world").toString() )
println("nBuffer object values are: " + Buffer("x", "y", "z").toString())
println("nIndexedSeq object values are: " +IndexedSeq(1.0, 2.0).toString())
println("nIndexedSeq object values are: " +LinearSeq("a", "b", "c").toString())
println("n===============================================================================")
}
}
Out put:
Collection API First Example =============================================================================== Traversable object values are: List(1, 2, 3) Iterable object values are: List(x, y, z) Map object values are: Map(x -> 24, y -> 25, z -> 26) Set object values are: Set(java.awt.Color[r=255,g=0,b=0], java.awt.Color[r=0,g=255,b=0], java.awt.Color[r=0,g=0,b=255]) SortedSet object values are: TreeSet(hello, world) Buffer object values are: ArrayBuffer(x, y, z) IndexedSeq object values are: Vector(1.0, 2.0) IndexedSeq object values are: MutableList(a, b, c) ===============================================================================
Operations in Class Traversable :
1) The only abstract operation in Traversable is foreach
def foreach[U](f: Elem => U)
Here f is place holders, to each element.
The type of the operation is Elem => U,
where Elem is the type of the collection’s elements
and U is an arbitrary result type.
CollectionAPITraversable.scala
import scala.Traversable
object CollectionAPITraversable {
def main(args: Array[String]) {
println("Collection API Traversable foreach Example")
println("===============================================================================")
println(Upper.upper("A", "First", "Scala", "Program"))
println("n===============================================================================")
}
}
object Upper {
def upper(strings: String*) = strings.map(_.toUpperCase()).foreach(println(_))
}
Out Put:
Collection API Traversable foreach Example =============================================================================== A FIRST SCALA PROGRAM () ===============================================================================
Example2
import scala.Traversable
object CollectionAPITraversable {
def main(args: Array[String]) {
val ls = List(1, "two", 3.1, '4')
ls.foreach(println(_))
}
}
Out Put:
1 two 3.1 4
Example3
object CollectionAPITraversable {
def main(args: Array[String]) {
val names = List("Jhon", "Bent", "Kent", "Juliat", "Smith")
for( name <- names if name.startsWith("J"))
println( name )
}
}
Out Put:
Jhon Juliat
More Emples
object CollectionAPITraversable {
def main(args: Array[String]) {
println("===================================================")
println(" Example 1 ")
println("===================================================")
val names = List("Jhon", "Bent", "Kent", "Juliat", "Smith")
names.foreach( i => println(i.toUpperCase()) )
println("===================================================")
println(" Example 2 ")
println("===================================================")
names.foreach( i => if( i.startsWith("S"))
println(i.toUpperCase()))
println("===================================================")
println(" Example 3 ")
println("===================================================")
val ls = List((1, "a"), (2, "b"), (3, "c"));
ls.foreach{ i => val(x, y) = i
println( x +" "+ y)}
println("===================================================")
}
}
Out Put:
===================================================
Example 1
===================================================
JHON
BENT
KENT
JULIAT
SMITH
===================================================
Example 2
===================================================
SMITH
===================================================
Example 3
===================================================
1 a
2 b
3 c
===================================================
*** Venkat – Happy leaning ****