28.08.2016 Views

scala_tutorial

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Scala<br />

// use two or more Maps with ++ as operator<br />

var colors = colors1 ++ colors2<br />

println( "colors1 ++ colors2 : " + colors )<br />

// use two maps with ++ as method<br />

colors = colors1.++(colors2)<br />

println( "colors1.++(colors2)) : " + colors )<br />

}<br />

}<br />

Save the above program in Demo.<strong>scala</strong>. The following commands are used to compile and<br />

execute this program.<br />

\><strong>scala</strong>c Demo.<strong>scala</strong><br />

\><strong>scala</strong> Demo<br />

Output<br />

colors1 ++ colors2 : Map(blue -> #0033FF, azure -> #F0FFFF,<br />

peru -> #CD853F, yellow -> #FFFF00, red -> #FF0000)<br />

colors1.++(colors2)) : Map(blue -> #0033FF, azure -> #F0FFFF,<br />

peru -> #CD853F, yellow -> #FFFF00, red -> #FF0000)<br />

Print Keys and Values from a Map<br />

You can iterate through the keys and values of a Map using “foreach” loop. Here, we used<br />

method foreach associated with iterator to walk through the keys. Following is the example<br />

program.<br />

object Demo {<br />

def main(args: Array[String]) {<br />

val colors = Map("red" -> "#FF0000",<br />

"azure" -> "#F0FFFF",<br />

"peru" -> "#CD853F")<br />

125

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!