Scala Learning Note (02) tuple tuple, array, MAP, file read/write, Web crawl example

Source: Internet
Author: User
Tags array example

Package Yjmyzzimport Java.io.PrintWriterimport Java.util.Dateimport scala.io.Sourceobject ScalaApp02 {def main (args: Array[string]) {tupledemo println mapdemo println arraydemo println filewriteandread println (getu     Rlcontent ("http://www.cnblogs.com/yjmyzz/")}/** * Tuple example */def Tupledemo = {//val represents a constant (equivalent to final in Java), VAR represents a variable    Val tuple = ("Jimmy", +, New Date ())//This is more concise than the tuple<t> in C #//print the first three elements, note that the tuple subscript is starting from 1 println (tuple._1) println (tuple._2) println (tuple._3)}/** * Array example */def Arraydemo = {var Myarr = array (5, 4, 3, 2, 1)//Note  Meaning: There is no need for the New keyword for (i <-Myarr) println (i) println//Find out the even var myarr in even = myarr.filter (i = i% 2 = = 0) The simplicity of this syntax is fast catching up to C # LINQ for (i <-even) println (i) println scala.util.Sorting.quickSort (even)//sort for (i &L    t;-even) println (i)}//map example Def Mapdemo = {var myMap = Map ("Jimmy", 1), ("Mike", 2), ("John", 3));  Traverse for ((K, v) <-MyMap) {    println ("key:" + K + "\t,value=" + V); } println println ("All keys =")//Traverse key for (K <-Mymap.keys) {println (k)} println//Times Calendar value println ("All values =") for (v <-mymap.values) {println (v)} println//Traverse key and value, and A notation (using placeholder notation "_") println ("All keys =") for ((K, _) <-MyMap) println (k) println println ("All Values =&G T ") for ((_, V) <-myMap) println (v)}/** * File read/write example */def Filewriteandread = {val FileName =" scalatest.t    XT "//write file var writer = new PrintWriter (fileName) writer.write (" Hello scala\n ") writer.write (" Hello Spark ")    Writer.close ()//Read file var filecontent = Source.fromfile (fileName);  println (filecontent.mkstring)}/** * Get page content * @param URL * @return */def geturlcontent (url:string): String = {source.fromurl (URL). mkstring}}

As you can see from the example code above, Scala has a very strong type derivation capability, and in most cases, without specifying a variable type, the compiler can deduce the variable type correctly based on the value of the variable, which is no less than C #

Scala Learning Note (02) tuple tuple, array, MAP, file read/write, Web crawl example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.