28.08.2016 Views

scala_tutorial

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Scala<br />

val nums: Map[Int, Int] = Map()<br />

println( "Keys in colors : " + colors.keys )<br />

println( "Values in colors : " + colors.values )<br />

println( "Check if colors is empty : " + colors.isEmpty )<br />

println( "Check if nums is empty : " + nums.isEmpty )<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 />

Keys in colors : Set(red, azure, peru)<br />

Values in colors : MapLike(#FF0000, #F0FFFF, #CD853F)<br />

Check if colors is empty : false<br />

Check if nums is empty : true<br />

Concatenating Maps<br />

You can use either ++ operator or Map.++() method to concatenate two or more Maps, but<br />

while adding Maps it will remove duplicate keys.<br />

Try the following example program to concatenate two Maps.<br />

object Demo {<br />

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

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

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

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

val colors2 = Map("blue" -> "#0033FF",<br />

"yellow" -> "#FFFF00",<br />

"red" -> "#FF0000")<br />

124

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

Saved successfully!

Ooh no, something went wrong!