GithupプロジェクトLearningSparkコード説明(十)

2329 ワード

package scalademo

import scala.io.Source

/**
  *           scala     ,        spark                  ,       
  *              ,          
  *
  *    :
  * 1  object         
  * 2                  ,             
  * 3                  
  * 4                   
  *
  */

object ScalaGrammar{

  def main(args: Array[String]): Unit = {
    val scalaGrammar = new ScalaGrammar()
    scalaGrammar.setDemo()
  }
}
class ScalaGrammar {

  /**
    *   0-100  
    */
  def sumCount(): Unit ={
    val a = (0 to 100)
    val count = a.reduce(_+_)
    println(count)

    val b = a.foldLeft(0)((x,y)=>x+y)
    println(b)
  }

  /**
    *        ,   scala   new   ,                     
    *       apply             。
    */
  def listDemo(): Unit ={

    val oneTow = List(1,2)
    val threeFour = List(3,4)
    //:::           ,   scala       
    val oneTwoThreeFour = oneTow:::threeFour
    println("oneTow:"+oneTow)
    println("threeFour:"+threeFour)
    println("oneTwoThreeFour:"+oneTwoThreeFour)

    /**
      *            '::'                  
      */
    val twoThree = List(2,3)
    val oneTwoThree = 1::twoThree
    println(oneTwoThree)

  }

  /**
    *            ,                ,            ,        
    */
  def tupleDemo(): Unit ={
    //      
    val pair = (99,"Luftballons")
    //  99
    println(pair._1)
    //  "Luftballons"
    println(pair._2)
  }

  /**
    *    set   ,     java      ,       ,       Set,                   
    *
    */
  def setDemo(): Unit ={
    import scala.collection.immutable.Set
    val jetSet = Set("Boeing","Airbus")
    val set2 = jetSet+"lean"
    println(set2)
  }

  /**
    *        Map   ,                +=  (add  ),         add  
    */
  def mapDemo(): Unit ={
    import scala.collection.mutable.Map
    val treasureMap = Map[Int,String]()
    treasureMap += (1->"a")
    treasureMap += (2->"b")
    treasureMap += (3->"c")
  }


  /**
    *                      var   ,   var           ,  var          
    *                  
    *
    * @param array
    */
  def functonStyle(array:Array[String]): Unit ={
    //    
    var i = 0
    while (i < array.size){
      println(array(i))
      i+=1
    }

    //   
    for(arr