v Loop A Collection - Scala

Loop A Collection

If you want to learn more, check out Scala Cookbook and Programming in Scala.

Create A Vector Collection

val vector = Vector("Male", 2, true)

Loop Over The Collection

// For each item in the collection, print the class type of the element
vector.foreach((i: Any) => println(i, i.getClass.getSimpleName))
(Male,String)
(2,Integer)
(true,Boolean)
// For each item in the collection
vector.foreach {
    // If one of these, print "Man"
    case "Male" | "M" | "Man" | "Gentleman" | "Boy" => println("Man")
    // For everything else, print "Something Else"
    case _ => println("Something Else")
}
Man
Something Else
Something Else