Scala闆嗗悎绫诲瀷璇﹁В

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Scala闆嗗悎绫诲瀷璇﹁В相关的知识,希望对你有一定的参考价值。

  • 鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%9f%a5%e7%9c%8b' title='鏌ョ湅'>鏌ョ湅   鏁村瀷   ++   first   涓嶅悓   head   lis   

    Scala闆嗗悎

    Scala鎻愪緵浜嗕竴濂楀緢濂界殑闆嗗悎瀹炵幇锛屾彁渚涗簡涓€浜涢泦鍚堢被鍨嬬殑鎶借薄銆?/p>

    Scala 闆嗗悎鍒嗕负鍙彉鐨勫拰涓嶅彲鍙樼殑闆嗗悎銆?/p>

    鍙彉闆嗗悎鍙互鍦ㄩ€傚綋鐨勫湴鏂硅鏇存柊鎴栨墿灞曘€傝繖鎰忓懗鐫€浣犲彲浠ヤ慨鏀癸紝娣诲姞锛岀Щ闄や竴涓泦鍚堢殑鍏冪礌銆傝€屼笉鍙彉闆嗗悎绫绘案杩滀笉浼氭敼鍙樸€備笉杩囷紝浣犱粛鐒跺彲浠ユā鎷熸坊鍔狅紝绉婚櫎鎴栨洿鏂版搷浣溿€備絾鏄繖浜涙搷浣滃皢鍦ㄦ瘡涓€绉嶆儏鍐典笅閮借繑鍥炰竴涓柊鐨勯泦鍚堬紝鍚屾椂浣垮師鏉ョ殑闆嗗悎涓嶅彂鐢熸敼鍙樸€?/p>

     

    闆嗗悎涓熀鏈粨鏋勶細

     鎶€鏈垎浜浘鐗? src=

     

     

    鍑犵甯哥敤闆嗗悎绫诲瀷绀轰緥锛?/p>

     

    1.  
      // 瀹氫箟鏁村瀷 List
    2.  
      val x = List(1, 2, 3, 4)
    3.  
       
    4.  
      // 瀹氫箟 Set
    5.  
      var x = Set(1, 3, 5, 7)
    6.  
       
    7.  
      // 瀹氫箟 Map
    8.  
      val x = Map("one" -> 1, "two" -> 2, "three" -> 3)
    9.  
       
    10.  
      // 鍒涘缓涓や釜涓嶅悓绫诲瀷鍏冪礌鐨勫厓缁?/span>
    11.  
      val x = (10, "Runoob")
    12.  
       
    13.  
      // 瀹氫箟 Option
    14.  
      val x: Option[Int] = Some(5)

     

     

     

    Scala List(鍒楄〃)

    Scala 鍒楄〃绫讳技浜庢暟缁勶紝瀹冧滑鎵€鏈夊厓绱犵殑绫诲瀷閮界浉鍚岋紝浣嗘槸瀹冧滑涔熸湁鎵€涓嶅悓锛氬垪琛ㄦ槸涓嶅彲鍙樼殑锛屽€间竴鏃﹁瀹氫箟浜嗗氨涓嶈兘鏀瑰彉锛屽叾娆″垪琛?鍏锋湁閫掑綊鐨勭粨鏋勶紙涔熷氨鏄摼鎺ヨ〃缁撴瀯锛夎€屾暟缁勪笉鏄€?/p>

    鍒楄〃鐨勫厓绱犵被鍨?T 鍙互鍐欐垚 List[T]銆備緥濡傦紝浠ヤ笅鍒楀嚭浜嗗绉嶇被鍨嬬殑鍒楄〃锛?/p>

     

    1.  
      // 瀛楃涓插垪琛?/span>
    2.  
      val site: List[String] = List("Sina", "Google", "Baidu")
    3.  
       
    4.  
      // 鏁村瀷鍒楄〃
    5.  
      val nums: List[Int] = List(1, 2, 3, 4)
    6.  
       
    7.  
      // 绌哄垪琛?/span>
    8.  
      val empty: List[Nothing] = List()
    9.  
       
    10.  
      // 浜岀淮鍒楄〃
    11.  
      val dim: List[List[Int]] =
    12.  
      List(
    13.  
      List(1, 0, 0),
    14.  
      List(0, 1, 0),
    15.  
      List(0, 0, 1)
    16.  
      )

     

     

    鏋勯€犲垪琛ㄧ殑涓や釜鍩烘湰鍗曚綅鏄?nbsp;Nil 鍜?nbsp;::

    Nil 涔熷彲浠ヨ〃绀轰负涓€涓┖鍒楄〃銆?/p>

    浠ヤ笂瀹炰緥鎴戜滑鍙互鍐欐垚濡備笅鎵€绀猴細锛?:锛夌鍙疯繛鎺ラ『搴忔槸浠庡彸鍒板乏

     

    1.  
      // 瀛楃涓插垪琛?/span>
    2.  
      val site = "Sina" :: ("Google" :: ("Baidu" :: Nil))
    3.  
       
    4.  
      // 鏁村瀷鍒楄〃
    5.  
      val nums = 1 :: (2 :: (3 :: (4 :: Nil)))
    6.  
       
    7.  
      // 绌哄垪琛?/span>
    8.  
      val empty = Nil
    9.  
       
    10.  
      // 浜岀淮鍒楄〃
    11.  
      val dim = (1 :: (0 :: (0 :: Nil))) ::
    12.  
      (0 :: (1 :: (0 :: Nil))) ::
    13.  
      (0 :: (0 :: (1 :: Nil))) :: Nil



    鍩烘湰鎿嶄綔

    Scala鍒楄〃鏈変笁涓熀鏈搷浣滐細

     

    1. head 杩斿洖鍒楄〃绗竴涓厓绱?/li>
    2. tail 杩斿洖涓€涓垪琛紝鍖呭惈闄や簡绗竴鍏冪礌涔嬪鐨勫叾浠栧厓绱狅細锛堟槸涓€涓垪琛ㄦ垨鑰匩il锛?/li>
    3. isEmpty 鍦ㄥ垪琛ㄤ负绌烘椂杩斿洖true

     

     

    瀵逛簬Scala鍒楄〃鐨勪换浣曟搷浣滈兘鍙互浣跨敤杩欎笁涓熀鏈搷浣滄潵琛ㄨ揪銆備唬鐮佺ず渚嬪涓?

     

    1.  
      val site = "Sina" :: ("Google" :: ("Baidu" :: Nil))
    2.  
      val nums = Nil
    3.  
      //鎵撳嵃娴嬭瘯head銆乼ail銆乮sEmpty鍔熻兘
    4.  
      println(site.head)
    5.  
      println(site.tail)
    6.  
      println(site.isEmpty)
    7.  
      println(nums.isEmpty)

    鎵撳嵃缁撴灉涓猴細

     

    1.  
      Sina
    2.  
      List(Google, Baidu)
    3.  
      false
    4.  
      true

     

    杩炴帴鍒楄〃

    浣犲彲浠ヤ娇鐢?nbsp; :::  銆?nbsp;List.:::()  鎴?nbsp; List.concat()  鏂规硶鏉ヨ繛鎺ヤ袱涓垨澶氫釜鍒楄〃銆?/p>

    浠g爜绀轰緥濡備笅:

     

    1.  
      val site1 = "Sina" :: ("Google" :: ("Baidu" :: Nil))
    2.  
      val site2 = "Facebook" :: ("Taobao" :: Nil)
    3.  
       
    4.  
      // 浣跨敤 ::: 杩愮畻绗?/span>
    5.  
      var combine = site1 ::: site2
    6.  
      println( "site1 ::: site2 : " + combine )
    7.  
       
    8.  
      // 浣跨敤 list.:::() 鏂规硶锛屾敞鎰忥細缁撴灉涓簊ite2鍒楄〃鍏冪礌鍦ㄥ墠锛宻ite1鍦ㄥ悗
    9.  
      combine = site1.:::(site2)
    10.  
      println( "site1.:::(site2) : " + combine )
    11.  
       
    12.  
      // 浣跨敤 concat 鏂规硶
    13.  
      combine = List.concat(site1, site2)
    14.  
      println( "List.concat(site1, site2) : " + combine )
    15.  
      }

     

    鎵撳嵃缁撴灉锛?/p>

     

    1.  
      site1 ::: site2 : List(Sina, Google, Baidu, Facebook, Taobao)
    2.  
      site1.:::(site2) : List(Facebook, Taobao, Sina, Google, Baidu)
    3.  
      List.concat(site1, site2) : List(Sina, Google, Baidu, Facebook, Taobao)


     

    List.fill()

    鎴戜滑鍙互浣跨敤 List.fill() 鏂规硶鏉ュ垱寤轰竴涓寚瀹氶噸澶嶆暟閲忕殑鍏冪礌鍒楄〃锛?/p>

     

    1.  
      val site = List.fill(3)("Baidu") // 閲嶅 Runoob 3娆?/span>
    2.  
      println( "site : " + site )
    3.  
       
    4.  
      val num = List.fill(10)(2) // 閲嶅鍏冪礌 2, 10 娆?/span>
    5.  
      println( "num : " + num )

     

    鎵撳嵃缁撴灉涓猴細

     

    1.  
      site : List(Baidu, Baidu, Baidu)
    2.  
      num : List(2, 2, 2, 2, 2, 2, 2, 2, 2, 2)



    List.tabulate()

    List.tabulate() 鏂规硶鏄€氳繃缁欏畾鐨勫嚱鏁版潵鍒涘缓鍒楄〃銆?/p>

    鏂规硶鐨勭涓€涓弬鏁颁负鍏冪礌鐨勬暟閲忥紝鍙互鏄簩缁寸殑锛岀浜屼釜鍙傛暟涓烘寚瀹氱殑鍑芥暟锛屾垜浠€氳繃鎸囧畾鐨勫嚱鏁拌绠楃粨鏋滃苟杩斿洖鍊兼彃鍏ュ埌鍒楄〃涓紝璧峰鍊间负 0锛屽疄渚嬪涓嬶細

     

    1.  
      // 閫氳繃缁欏畾鐨勫嚱鏁板垱寤?5 涓厓绱?/span>
    2.  
      val squares = List.tabulate(6)(n => n * n)
    3.  
      println( "涓€缁?: " + squares )
    4.  
       
    5.  
      // 鍒涘缓浜岀淮鍒楄〃
    6.  
      val mul = List.tabulate( 4,5 )( _ * _ )
    7.  
      println( "浜岀淮 : " + mul )

     

    鎵撳嵃缁撴灉涓猴細

     

    1.  
      涓€缁?: List(0, 1, 4, 9, 16, 25)
    2.  
      浜岀淮 : List(List(0, 0, 0, 0, 0), List(0, 1, 2, 3, 4), List(0, 2, 4, 6, 8), List(0, 3, 6, 9, 12))



    List.reverse

    List.reverse 鐢ㄤ簬灏嗗垪琛ㄧ殑椤哄簭鍙嶈浆锛屽疄渚嬪涓嬶細

     

    1.  
      val site = "Sina" :: ("Google" :: ("Baidu" :: Nil))
    2.  
      println( "鍙嶈浆鍓?: " + site )
    3.  
      println( "鍙嶈浆鍓?: " + site.reverse )

     

    鎵撳嵃缁撴灉涓猴細

     

    1.  
      鍙嶈浆鍓?: List(Sina, Google, Baidu)
    2.  
      鍙嶈浆鍓?: List(Baidu, Google, Sina)



    鍒楄〃缂撳瓨(ListBuffer)

    List绫昏兘澶熸彁渚涘鍒楄〃澶撮儴锛岃€岄潪灏鹃儴鐨勫揩閫熻闂€傚鏋滈渶瑕佸悜缁撳熬娣诲姞瀵硅薄锛屽垯闇€瑕佸厛瀵硅〃澶村墠缂€鍏冪礌鏂瑰紡鍙嶅悜鏋勯€犲垪琛紝瀹屾垚涔嬪悗鍐嶈皟鐢╮everse銆?/p>

    涓婅堪闂鍙︿竴绉嶈В鍐虫柟寮忓氨鏄娇鐢↙istBuffer锛岃繖鍙互閬垮厤reverse鎿嶄綔銆侺istBuffer鏄彲鍙樺璞★紝瀹冨彲浠ユ洿楂樻晥鐨勯€氳繃娣诲姞鍏冪礌鏉ユ瀯寤哄垪琛ㄣ€?/p>

    浣跨敤ListBuffer鏇夸唬List鍙︿竴涓悊鐢辨槸閬垮厤鏍堟孩鍑洪闄┿€?/p>

     

    ListBuffer浣跨敤绀轰緥锛?/p>

     

    1.  
      val buf: ListBuffer[Int] = new ListBuffer[Int]
    2.  
      //寰€鍚庢坊鍔?/span>
    3.  
      buf += 1
    4.  
      buf += 2
    5.  
       
    6.  
      //鍓嶇紑娣诲姞
    7.  
      val buf2 = 3 +: buf
    8.  
      println(buf2.toString())
    9.  
       
    10.  
      //ListBuffer杞琇ist
    11.  
      println(buf2.toList.toString())



    List甯哥敤鏂规硶

     

    • 鍙傝€冨簳閮ㄩ檮褰?

     

     

     

     

    Scala闃熷垪鍜屾爤

    闃熷垪

    濡傛灉浣犻渶瑕佸厛杩涘厛鍑哄簭鍒楋紝浣犲彲浠ヤ娇鐢≦ueue(闃熷垪)銆係cala闆嗗悎鎻愪緵浜嗗彲鍙樺拰涓嶅彲鍙樼殑Queue銆?/p>

     

    涓嶅彲鍙楺ueue浠g爜绀轰緥锛?/p>

     

    1.  
      //浣跨敤浼寸敓瀵硅薄鍒涘缓涓€涓猶ueue
    2.  
      val que = Queue[Int]()
    3.  
       
    4.  
      //浣跨敤enqueue涓轰笉鍙彉闃熷垪娣诲姞鍏冪礌
    5.  
      val que1 = que.enqueue(1)
    6.  
       
    7.  
      //寰€闃熷垪娣诲姞澶氫釜鍏冪礌锛屾妸闆嗗悎浣滀负enqueue鐨勫弬鏁?/span>
    8.  
      val que2 = que1.enqueue(List(2,3,4,5))
    9.  
       
    10.  
      //浠庨槦鍒楀ご閮ㄧЩ闄ゅ厓绱狅紝浣跨敤dequeue
    11.  
      //绗竴涓弬鏁颁负澶撮儴绉婚櫎鐨勫厓绱狅紝绗簩涓弬鏁颁负鍓╀笅鐨勯槦鍒?/span>
    12.  
      val (elem1,que3) = que2.dequeue
    13.  
       
    14.  
      //鎵撳嵃绉婚櫎鐨勫厓绱?/span>
    15.  
      println(elem1)
    16.  
       
    17.  
      //鎵撳嵃鍓╀笅鐨勯槦鍒?/span>
    18.  
      println(que3)

     

    鎵撳嵃缁撴灉涓猴細

     

    1.  
      1
    2.  
      Queue(2, 3, 4, 5)



    鍙彉Queue浠g爜绀轰緥锛?/p>

     

     

    1.  
      //浣跨敤浼寸敓瀵硅薄鍒涘缓涓€涓彲鍙榪ueue
    2.  
      var que = scala.collection.mutable.Queue[String]()
    3.  
       
    4.  
      //浣跨敤 += 绗﹀彿娣诲姞鍗曚釜鍏冪礌
    5.  
      que += "A"
    6.  
       
    7.  
      //浣跨敤 ++= 绗﹀彿娣诲姞澶氫釜鍏冪礌
    8.  
      que ++= List("B","C","D")
    9.  
       
    10.  
      //浣跨敤dequeue绉婚櫎澶撮儴鍏冪礌
    11.  
      val a = que.dequeue
    12.  
       
    13.  
      //鎵撳嵃绉婚櫎鐨勫厓绱?/span>
    14.  
      println(a)
    15.  
       
    16.  
      //鎵撳嵃闃熷垪涓墿涓嬬殑鍏冪礌
    17.  
      print(que)

     

    鎵撳嵃缁撴灉锛?/p>

     

    1.  
      A
    2.  
      Queue(B, C, D)

     

     

    鏍?/h3>

    濡傛灉闇€瑕佺殑鏄悗杩涘厛鍑猴紝浣犲彲浠ヤ娇鐢⊿tack锛屽畠鍚屾牱鍦⊿cala鐨勯泦鍚堜腑鏈夊彲鍙樺拰涓嶅彲鍙樼増鏈€傚厓绱犵殑鎺ㄥ叆浣跨敤push锛屽脊鍑虹敤pop锛屽彧鑾峰彇鏍堥《鍏冪礌鑰屼笉绉婚櫎鍙互浣跨敤top銆?/p>

     

    鍙彉鏍堢ず渚嬶細

     

    1.  
      //浣跨敤Stack绫荤殑浼寸敓瀵硅薄鍒涘缓Stack瀵硅薄
    2.  
      var stack = scala.collection.mutable.Stack[Int]()
    3.  
       
    4.  
      //寰€鏍坰tack涓帇濡傚厓绱?/span>
    5.  
      stack.push(1)
    6.  
      stack.push(2)
    7.  
      stack.push(3)
    8.  
       
    9.  
      //鎵撳嵃鏌ョ湅鏍堝唴鍏冪礌
    10.  
      println(stack)
    11.  
       
    12.  
      //鑾峰彇鏍堥《鍏冪礌鐨勫€?/span>
    13.  
      val tval = stack.top
    14.  
      println("鏍堥《鍏冪礌涓?: " + tval)
    15.  
       
    16.  
      //绉婚櫎鏍堥《鍏冪礌
    17.  
      val pval = stack.pop()
    18.  
      println("绉婚櫎鐨勬爤椤跺厓绱犱负 : " + pval)
    19.  
       
    20.  
      //鎵撳嵃绉婚櫎鏍堥《鍏冪礌鍚庯紝鍓╀笅鐨勬爤鍐呭厓绱?/span>
    21.  
      println(stack)

     

    鎵撳嵃缁撴灉锛?/p>

     

    1.  
      Stack(3, 2, 1)
    2.  
      鏍堥《鍏冪礌涓?: 3
    3.  
      绉婚櫎鐨勬爤椤跺厓绱犱负 : 3
    4.  
      Stack(2, 1)



    闃熷垪鍜屾爤甯哥敤鎿嶄綔

     

    • 鍙傝€冨簳閮ㄩ檮褰曪細

     

     

     

     

    Scala Set(闆?

     

    1. Scala Set(闆?鏄病鏈夐噸澶嶇殑瀵硅薄闆嗗悎锛屾墍鏈夌殑鍏冪礌閮芥槸鍞竴鐨勩€?/li>
    2. Scala 闆嗗悎鍒嗕负鍙彉鐨勫拰涓嶅彲鍙樼殑闆嗗悎銆?/li>
    3. 榛樿鎯呭喌涓嬶紝Scala 浣跨敤鐨勬槸涓嶅彲鍙橀泦鍚堬紝濡傛灉鎯充娇鐢ㄥ彲鍙橀泦鍚堬紝闇€寮曠敤 scala.collection.mutable.Set 鍖呫€?/li>
    4. 榛樿寮曠敤 scala.collection.immutable.Set銆?/li>

     

     

    涓嶅彲鍙橀泦鍚堝疄渚嬪涓嬶細

     

    1.  
      val set = Set(1,2,3)
    2.  
      println(set.getClass.getName) //
    3.  
       
    4.  
      println(set.exists(_ % 2 == 0)) //true
    5.  
      println(set.drop(1)) //Set(2,3)

     

    鎵撳嵃缁撴灉涓猴細

     

    1.  
      scala.collection.immutable.Set$Set3
    2.  
      true
    3.  
      Set(2, 3)



    濡傛灉闇€瑕佷娇鐢ㄥ彲鍙橀泦鍚堥渶瑕佸紩鍏?scala.collection.mutable.Set锛?/p>

     

    1.  
      import scala.collection.mutable.Set // 鍙互鍦ㄤ换浣曞湴鏂瑰紩鍏?鍙彉闆嗗悎
    2.  
       
    3.  
      val mutableSet = Set(1,2,3)
    4.  
      println(mutableSet.getClass.getName) // scala.collection.mutable.HashSet
    5.  
      //寰€闆嗗悎鍐呮坊鍔犲厓绱?
    6.  
      mutableSet.add(4)
    7.  
      //鍒犻櫎鍊间负1鐨勫厓绱?/span>
    8.  
      mutableSet.remove(1)
    9.  
      //娣诲姞鍏冪礌5
    10.  
      mutableSet += 5
    11.  
      //鍒犻櫎鍊间负3鐨勫厓绱?/span>
    12.  
      mutableSet -= 3
    13.  
       
    14.  
      println(mutableSet) // Set(5, 3, 4)
    15.  
       
    16.  
      val another = mutableSet.toSet
    17.  
      println(another.getClass.getName) // scala.collection.immutable.Set

     

    娉ㄦ剰锛?nbsp;铏界劧鍙彉Set鍜屼笉鍙彉Set閮芥湁娣诲姞鎴栧垹闄ゅ厓绱犵殑鎿嶄綔锛屼絾鏄湁涓€涓潪甯稿ぇ鐨勫樊鍒€傚涓嶅彲鍙楽et杩涜鎿嶄綔锛屼細浜х敓涓€涓柊鐨剆et锛屽師鏉ョ殑set骞舵病鏈夋敼鍙橈紝杩欎笌List涓€鏍枫€?鑰屽鍙彉Set杩涜鎿嶄綔锛屾敼鍙樼殑鏄Set鏈韩锛屼笌ListBuffer绫讳技銆?/p>

     

    Set闆嗗悎鍩烘湰鎿嶄綔

    Scala Set闆嗗悎鏈変笁涓熀鏈搷浣滐細

     

    1. head 杩斿洖闆嗗悎绗竴涓厓绱?/li>
    2. tail 杩斿洖涓€涓泦鍚堬紝鍖呭惈闄や簡绗竴鍏冪礌涔嬪鐨勫叾浠栧厓绱?/li>
    3. isEmpty 鍦ㄩ泦鍚堜负绌烘椂杩斿洖true

     

    瀵逛簬Scala闆嗗悎鐨勪换浣曟搷浣滈兘鍙互浣跨敤杩欎笁涓熀鏈搷浣滄潵琛ㄨ揪銆?/p>

    浠g爜绀轰緥濡備笅:

     

    1.  
      val site = Set("Sina", "Google", "Baidu")
    2.  
      val nums: Set[Int] = Set()
    3.  
       
    4.  
      println( "head : " + site.head )
    5.  
      println( "tail : " + site.tail )
    6.  
      println( "isEmpty : " + site.isEmpty )
    7.  
      println( "isEmpty : " + nums.isEmpty )

     

    鎵撳嵃缁撴灉涓猴細

     

    1.  
      head : Sina
    2.  
      tail : Set(Google, Baidu)
    3.  
      isEmpty : false
    4.  
      isEmpty : true



    杩炴帴闆嗗悎

    浣犲彲浠ヤ娇鐢?nbsp;++ 杩愮畻绗︽垨 Set.++() 鏂规硶鏉ヨ繛鎺ヤ袱涓泦鍚堛€傚鏋滃厓绱犳湁閲嶅鐨勫氨浼氱Щ闄ら噸澶嶇殑鍏冪礌銆傚疄渚嬪涓嬶細

     

    1.  
      val site1 = Set("Sina", "Google", "Baidu")
    2.  
      val site2 = Set("Faceboook", "Taobao")
    3.  
       
    4.  
      // ++ 浣滀负杩愮畻绗︿娇鐢?/span>
    5.  
      var site = site1 ++ site2
    6.  
      println( "site1 ++ site2 : " + site )
    7.  
       
    8.  
      // ++ 浣滀负鏂规硶浣跨敤
    9.  
      site = site1.++(site2)
    10.  
      println( "site1.++(site2) : " + site )

     

    鎵撳嵃缁撴灉涓?

     

    1.  
      site1 ++ site2 : Set(Faceboook, Taobao, Sina, Google, Baidu)
    2.  
      site1.++(site2) : Set(Faceboook, Taobao, Sina, Google, Baidu)



     

    鏌ユ壘闆嗗悎涓渶澶т笌鏈€灏忓厓绱?/h3>

    浣犲彲浠ヤ娇鐢?nbsp;Set.min 鏂规硶鏉ユ煡鎵鹃泦鍚堜腑鐨勬渶灏忓厓绱狅紝浣跨敤 Set.max 鏂规硶鏌ユ壘闆嗗悎涓殑鏈€澶у厓绱犮€傚疄渚嬪涓嬶細

     

    1.  
      val num = Set(5,6,9,20,30,45)
    2.  
       
    3.  
      // 鏌ユ壘闆嗗悎涓渶澶т笌鏈€灏忓厓绱?/span>
    4.  
      println( "Set(5,6,9,20,30,45) 鏈€灏忓厓绱犳槸 : " + num.min )
    5.  
      println( "Set(5,6,9,20,30,45) 鏈€澶у厓绱犳槸 : " + num.max )

     

    鎵撳嵃缁撴灉涓猴細

     

    1.  
      Set(5,6,9,20,30,45) 鏈€灏忓厓绱犳槸 : 5
    2.  
      Set(5,6,9,20,30,45) 鏈€澶у厓绱犳槸 : 45



    浜ら泦

    浣犲彲浠ヤ娇鐢?nbsp;Set.& 鏂规硶鎴?nbsp;Set.intersect 鏂规硶鏉ユ煡鐪嬩袱涓泦鍚堢殑浜ら泦鍏冪礌銆傚疄渚嬪涓嬶細

     

    1.  
      val num1 = Set(5,6,9,20,30,45)
    2.  
      val num2 = Set(50,60,9,20,35,55)
    3.  
       
    4.  
      // 浜ら泦
    5.  
      println( "num1.&(num2) : " + num1.&(num2) )
    6.  
      println( "num1.intersect(num2) : " + num1.intersect(num2) )

     

    鎵撳嵃缁撴灉涓猴細

     

    1.  
      num1.&(num2) : Set(20, 9)
    2.  
      num1.intersect(num2) : Set(20, 9)

     

    Scala Set 甯哥敤鏂规硶

     

    • 鍙傝€冨簳閮ㄩ檮褰曪細

     

     

     

     

    Scala Map(鏄犲皠)

     

    1. Map(鏄犲皠)鏄竴绉嶅彲杩唬鐨勯敭鍊煎锛坘ey/value锛夌粨鏋勩€?/li>
    2. 鎵€鏈夌殑鍊奸兘鍙互閫氳繃閿潵鑾峰彇銆?/li>
    3. Map 涓殑閿兘鏄敮涓€鐨勩€?/li>
    4. Map 涔熷彨鍝堝笇琛紙Hash tables锛夈€?/li>
    5. Map 鏈変袱绉嶇被鍨嬶紝鍙彉涓庝笉鍙彉锛屽尯鍒湪浜庡彲鍙樺璞″彲浠ヤ慨鏀瑰畠锛岃€屼笉鍙彉瀵硅薄涓嶅彲浠ャ€?/li>
    6. 榛樿鎯呭喌涓?Scala 浣跨敤涓嶅彲鍙?Map銆傚鏋滀綘闇€瑕佷娇鐢ㄥ彲鍙橀泦鍚堬紝浣犻渶瑕佹樉寮忕殑寮曞叆 import scala.collection.mutable.Map 绫?/li>
    7. 鍦?Scala 涓?浣犲彲浠ュ悓鏃朵娇鐢ㄥ彲鍙樹笌涓嶅彲鍙?Map锛屼笉鍙彉鐨勭洿鎺ヤ娇鐢?Map锛屽彲鍙樼殑浣跨敤 mutable.Map銆?/li>

     

     

    浠ヤ笅瀹炰緥婕旂ず浜嗕笉鍙彉 Map 鐨勫簲鐢細

     

    1.  
      // 绌哄搱甯岃〃锛岄敭涓哄瓧绗︿覆锛屽€间负鏁村瀷
    2.  
      var A:Map[Char,Int] = Map()
    3.  
       
    4.  
      // Map 閿€煎婕旂ず
    5.  
      val lang= Map("Java" -> "Oracle", "C#" -> "Microsoft")
    6.  
      鎴栬€?/div>
    7.  
      val lang= Map(("Java","Oracle"), ("C#" , "Microsoft"))



    瀹氫箟 Map 鏃讹紝闇€瑕佷负閿€煎瀹氫箟绫诲瀷銆傚鏋滈渶瑕佹坊鍔?key-value 瀵癸紝鍙互浣跨敤 + 鍙凤紝濡備笅鎵€绀猴細

     

    A += (鈥榯鈥?->10 )

     

     

     

    Map 鍩烘湰鎿嶄綔

    Scala Map 鍑犵鍩烘湰鎿嶄綔锛歬eys銆乿alues銆乮sEmpty銆佽祴鍊硷紙鍙彉鏄犲皠锛?/p>

    浠g爜绀轰緥锛?/p>

    Keys:

     

    1.  
      val lang = Map("Java" -> "Oracle",
    2.  
      "C#" -> "Microsoft",
    3.  
      "Swift" -> "Apple")
    4.  
      Values:
    5.  
      val nums: Map[Int, Int] = Map()
    6.  
      println( "lang 涓殑閿负 : " + lang.keys )
    7.  
      println( "lang 涓殑鍊间负 : " + lang.values )

     

    isEmpty:

    1.  
      println( "lang 鏄惁涓虹┖ : " + lang.isEmpty )
    2.  
      println( "nums 鏄惁涓虹┖ : " + nums.isEmpty )

     

    keys鍜宨sEmpty鐨勬墦鍗扮粨鏋滀负锛?/p>

     

    1.  
      lang 涓殑閿负 : Set(Java, C#, Swift)
    2.  
      lang 涓殑鍊间负 : MapLike(Oracle, Microsoft, Apple)
    3.  
      lang 鏄惁涓虹┖ : false
    4.  
      nums 鏄惁涓虹┖ : true



    璧嬪€硷細

     

    1.  
      var lang= scala.collection.mutable.Map("Java" -> "Oracle", "C#" -> "Microsoft")
    2.  
      lang("Java") = "sun"
    3.  
      println(lang)

     

    鎵撳嵃缁撴灉涓猴細

     

    Map(C# -> Microsoft, Java -> sun)



     

    Map 鍚堝苟

    浣犲彲浠ヤ娇鐢?nbsp;++ 杩愮畻绗︽垨 Map.++() 鏂规硶鏉ヨ繛鎺ヤ袱涓?Map锛孧ap 鍚堝苟鏃朵細绉婚櫎閲嶅鐨?key銆備互涓嬫紨绀轰簡涓や釜 Map 鍚堝苟鐨勫疄渚?

     

    1.  
      val lang =Map("Java" -> "Oracle",
    2.  
      "C#" -> "Microsoft",
    3.  
      "Swift" -> "Apple")
    4.  
      val color = Map("blue" -> "#0033FF",
    5.  
      "yellow" -> "#FFFF00",
    6.  
      "red" -> "#FF0000")
    7.  
       
    8.  
      // ++ 浣滀负杩愮畻绗?/span>
    9.  
      var colors = lang ++ color
    10.  
      println( "lang ++ colors : " + colors )
    11.  
       
    12.  
      // ++ 浣滀负鏂规硶
    13.  
      colors = lang.++(colors)
    14.  
      println( "lang.++(colors)) : " + colors )

     

    鎵撳嵃缁撴灉涓猴細

     

    1.  
      lang ++ colors : Map(blue -> #0033FF, C# -> Microsoft, yellow -> #FFFF00, Java -> Oracle, red -> #FF0000, Swift -> Apple)
    2.  
      lang.++(colors)) : Map(blue -> #0033FF, C# -> Microsoft, yellow -> #FFFF00, Java -> Oracle, red -> #FF0000, Swift -> Apple)



    杈撳嚭 Map 鐨?nbsp;keys 鍜?nbsp;values

    浠ヤ笅閫氳繃 foreach 寰幆杈撳嚭 Map 涓殑 keys 鍜?values锛?/p>

     

    1.  
      val lang =Map("Java" -> "Oracle",
    2.  
      "C#" -> "Microsoft",
    3.  
      "Swift" -> "Apple")
    4.  
       
    5.  
      lang.keys.foreach{ i =>
    6.  
      print( "Key = " + i )
    7.  
      println(" Value = " + lang(i) )}

     

    鎵撳嵃缁撴灉涓猴細

     

    1.  
      Key = JavaValue = Oracle
    2.  
      Key = C#Value = Microsoft
    3.  
      Key = SwiftValue = Apple



    鏌ョ湅 Map 涓槸鍚﹀瓨鍦ㄦ寚瀹氱殑 Key

    浣犲彲浠ヤ娇鐢?nbsp;Map.contains 鏂规硶鏉ユ煡鐪?Map 涓槸鍚﹀瓨鍦ㄦ寚瀹氱殑 Key銆傚疄渚嬪涓嬶細

     

    1.  
      val lang =Map("Java" -> "Oracle",
    2.  
      "C#" -> "Microsoft",
    3.  
      "Swift" -> "Apple")
    4.  
       
    5.  
      if(lang.contains("Swift")) {
    6.  
      println(lang("Swift")+"鍒涢€犱簡 Swift 璇█")
    7.  
      }

     

    鎵撳嵃缁撴灉涓猴細

     

    Apple鍒涢€犱簡 Swift 璇█



    杩唬鏄犲皠

     

    1.  
      val lang = Map("Java" -> "Oracle", "C#" -> "Microsoft")
    2.  
      for ((k, v) <- lang) {
    3.  
      println(k + " : " + v)
    4.  
      }

     

    鎵撳嵃缁撴灉涓猴細

     

    1.  
      Java : Oracle
    2.  
      C# : Microsoft



    鏄犲皠涓璌锛孷鍙嶈浆

     

    1.  
      val lang = Map("Java" -> "Oracle", "C#" -> "Microsoft")
    2.  
      val lang2 = for ((k, v) <- lang) yield (v, k)
    3.  
      for ((k, v) <- lang2) {
    4.  
      println(k + " : " + v)
    5.  
      }

     

    鎵撳嵃缁撴灉涓猴細

     

    1.  
      Oracle : Java
    2.  
      Microsoft : C#



    宸叉帓搴忔槧灏?/h3>

     

    1.  
      //鎸夌収key鐨勫瓧鍏搁『搴忔帓搴忋€?/span>
    2.  
      val scores = scala.collection.immutable.SortedMap(("Bob", 8), ("Alice", 21), ("Fred", 17), ("Cindy", 15))
    3.  
      scores.foreach(person => println(person._1 + " : " + person._2))

     

    鎵撳嵃缁撴灉涓猴細

     

    1.  
      Alice : 21
    2.  
      Bob : 8
    3.  
      Cindy : 15
    4.  
      Fred : 17

     

    Scala Map甯哥敤鏂规硶

     

    • 鍙傝€冨簳閮ㄩ檮褰曪細

     

     

     

     

     

     

    Scala Tuple(鍏冪粍)

    涓庡垪琛ㄤ竴鏍凤紝鍏冪粍涔熸槸涓嶅彲鍙樼殑锛屼絾涓庡垪琛ㄤ笉鍚岀殑鏄厓缁勫彲浠ュ寘鍚笉鍚岀被鍨嬬殑鍏冪礌銆?/p>

    鍏冪粍鐨勫€兼槸閫氳繃灏嗗崟涓殑鍊煎寘鍚湪鍦嗘嫭鍙蜂腑鏋勬垚鐨勩€備緥濡傦細

     

     

    1.  
      //鍏冪粍涓畾涔変簡涓変釜鍏冪礌锛屽搴旂殑绫诲瀷鍒嗗埆涓篬Int, Double, java.lang.String]銆?/span>
    2.  
      val tuple1 = (1, 5.20, "Spark")
    3.  
      //鎴栬€?/span>
    4.  
      val tuple2 = new Tuple3(1,5.20,"Spark")

     

     

     

    鎴戜滑鍙互浣跨敤 tuple1._1 璁块棶绗竴涓厓绱狅紝 tuple1._2 璁块棶绗簩涓厓绱狅紝濡備笅鎵€绀猴細

     

    1.  
      //鍏冪粍涓畾涔変簡涓変釜鍏冪礌锛屽搴旂殑绫诲瀷鍒嗗埆涓篬Int, Double, java.lang.String]銆?/span>
    2.  
      val tuple1 = (1, 5.20, "Spark")
    3.  
       
    4.  
      println(tuple1._1 + " : " + tuple1._2 + " : " + tuple1._3)

     

    鎵撳嵃缁撴灉涓猴細

     

    1 : 5.2 : Spark



    杩唬鍏冪粍

    浣犲彲浠ヤ娇鐢?nbsp;Tuple.productIterator() 鏂规硶鏉ヨ凯浠h緭鍑哄厓缁勭殑鎵€鏈夊厓绱狅細

     

    1.  
      //鍏冪粍涓畾涔変簡涓変釜鍏冪礌锛屽搴旂殑绫诲瀷鍒嗗埆涓篬Int, Double, java.lang.String]銆?/span>
    2.  
      val tuple1 = (1, 5.20, "Spark")
    3.  
       
    4.  
      tuple1.productIterator.foreach{i => println("value : " + i)}

     

    鎵撳嵃缁撴灉涓猴細

     

    1.  
      value : 1
    2.  
      value : 5.2
    3.  
      value : Spark

     

     

    鍏冪粍杞负瀛楃涓?/h3>

    浣犲彲浠ヤ娇鐢?nbsp;Tuple.toString() 鏂规硶灏嗗厓缁勭殑鎵€鏈夊厓绱犵粍鍚堟垚涓€涓瓧绗︿覆锛屽疄渚嬪涓嬶細

     

    1.  
      //鍏冪粍涓畾涔変簡涓変釜鍏冪礌锛屽搴旂殑绫诲瀷鍒嗗埆涓篬Int, Double, java.lang.String]銆?/span>
    2.  
      val tuple1 = (1, 5.20, "Spark")
    3.  
       
    4.  
      println(tuple1.toString())

     

    鎵撳嵃缁撴灉涓猴細

     

    (1,5.2,Spark)



    鍏冪礌浜ゆ崲

    浣犲彲浠ヤ娇鐢?nbsp;Tuple.swap 鏂规硶鏉ヤ氦鎹㈠厓缁勭殑鍏冪礌銆傚涓嬪疄渚嬶細

     

    1.  
      //鍏冪粍涓畾涔変簡涓や釜鍏冪礌锛屽搴旂殑绫诲瀷鍒嗗埆涓篬Int, java.lang.String]銆?/span>
    2.  
      val tuple1 = (1, "Spark")
    3.  
      //娉ㄦ剰锛歴wap鍑芥暟鍙兘鐢ㄤ簬涓や釜鍏冪礌鍏冪粍
    4.  
      println(tuple1.swap)

     

    鎵撳嵃缁撴灉涓猴細

     

    (Spark,1)

     

     

    浣跨敤妯″紡鍖归厤鑾峰彇鍏冪粍

    浠g爜绀轰緥锛?/p>

     

    1.  
      val t = (1, 3.14, "Fred")
    2.  
      val (first, second, third) = t
    3.  
      println(first + " : " + second + " : " + third)

     

    鎵撳嵃缁撴灉锛?/p>

     

    1 : 3.14 : Fred



    鎷夐摼鎿嶄綔

     

    1.  
      val num = Array(1, 2, 3)
    2.  
      val str = Array("first", "second", "third")
    3.  
      val com = num.zip(str).toMap
    4.  
      println(com(1)+" : "+com(2)+" : "+com(3))

     

    鎵撳嵃缁撴灉涓猴細

     

    first : second : third

     

    鍏冪粍甯哥敤鏂规硶

     

    • 鍙傝€冨簳閮ㄩ檮褰曪細

     

     

     

     

    Scala Option(閫夐」)

    Scala Option(閫夐」)绫诲瀷鐢ㄦ潵琛ㄧず涓€涓€兼槸鍙€夌殑锛堟湁鍊兼垨鏃犲€?銆?/p>

    Option[T] 鏄竴涓被鍨嬩负 T 鐨勫彲閫夊€肩殑瀹瑰櫒锛?濡傛灉鍊煎瓨鍦紝 Option[T] 灏辨槸涓€涓?Some[T] 锛屽鏋滀笉瀛樺湪锛?Option[T] 灏辨槸瀵硅薄 None 銆?/p>

    浠g爜绀轰緥锛?/p>

     

    1.  
      val myMap: Map[String, String] = Map("key1" -> "value")
    2.  
      val value1: Option[String] = myMap.get("key1")
    3.  
      val value2: Option[String] = myMap.get("key2")
    4.  
       
    5.  
      println(value1) // Some("value1")
    6.  
      println(value2) // None

     

    鎵撳嵃缁撴灉涓猴細

     

    1.  
      Some(value)
    2.  
      None

     

    Option 鏈変袱涓瓙绫诲埆锛屼竴涓槸 Some锛屼竴涓槸 None锛屽綋浠栧洖浼?Some 鐨勬椂鍊欙紝浠h〃杩欎釜鍑藉紡鎴愬姛鍦扮粰浜嗕綘涓€涓?String锛岃€屼綘鍙互閫忚繃 get() 杩欎釜鍑藉紡鎷垮埌閭d釜 String锛屽鏋滀粬杩斿洖鐨勬槸 None锛屽垯浠h〃娌℃湁瀛楃涓插彲浠ョ粰浣犮€?/p>

     

     

     

    閫氳繃妯″紡鍖归厤鏉ヨ緭鍑哄尮閰嶅€笺€?/p>

    浠g爜绀轰緥锛?/p>

     

    1.  
      val lang =Map("Java" -> "Oracle",
    2.  
      "C#" -> "Microsoft",
    3.  
      "Swift" -> "Apple")
    4.  
       
    5.  
      println(show(lang.get("Swift")))
    6.  
      println(show(lang.get("Scala")))
    7.  
       
    8.  
      def show(x:Option[String]) = x match {
    9.  
      case Some(s) => s
    10.  
      case None => "?"
    11.  
      }

     

    鎵撳嵃缁撴灉涓猴細

     

    1.  
      Apple
    2.  
      ?

     

     

    getOrElse() 鏂规硶

    浣犲彲浠ヤ娇鐢?getOrElse() 鏂规硶鏉ヨ幏鍙栧厓缁勪腑瀛樺湪鐨勫厓绱犳垨鑰呬娇鐢ㄥ叾榛樿鐨勫€硷紝瀹炰緥濡備笅锛?/p>

     

    1.  
      val a:Option[Int] = Some(5)
    2.  
      val b:Option[Int] = Some(7)
    3.  
      val c:Option[Int] = None
    4.  
       
    5.  
      println("a.getOrElse(0): " + a.getOrElse(0) )
    6.  
      println("b.getOrElse(1): " + b.getOrElse(1) )
    7.  
      println("c.getOrElse(10): " + c.getOrElse(10))

     

    鎵撳嵃缁撴灉涓猴細

     

    1.  
      a.getOrElse(0): 5
    2.  
      b.getOrElse(1): 7
    3.  
      c.getOrElse(10): 10



    isEmpty() 鏂规硶

    浣犲彲浠ヤ娇鐢?isEmpty() 鏂规硶鏉ユ娴嬪厓缁勪腑鐨勫厓绱犳槸鍚︿负 None锛屽疄渚嬪涓嬶細

     

    1.  
      val a:Option[Int] = Some(5)
    2.  
      val b:Option[Int] = None
    3.  
      println("a.isEmpty: " + a.isEmpty )
    4.  
      println("b.isEmpty: " + b.isEmpty )

     

    鎵撳嵃缁撴灉涓猴細

     

    1.  
      a.isEmpty: false
    2.  
      b.isEmpty: true

     

    Scala Option 甯哥敤鏂规硶

     

    • 鍙傝€冨簳閮ㄩ檮褰曪細

     

     

     

    Scala Iterator(杩唬鍣?

     

    1. Scala Iterator锛堣凯浠e櫒锛変笉鏄竴涓泦鍚堬紝瀹冩槸涓€绉嶇敤浜庤闂泦鍚堢殑鏂规硶銆?/li>
    2. 杩唬鍣?it 鐨勪袱涓熀鏈搷浣滄槸 next 鍜?nbsp;hasNext銆?/li>
    3. 璋冪敤 it.next() 浼氳繑鍥炶凯浠e櫒鐨勪笅涓€涓厓绱狅紝骞朵笖鏇存柊杩唬鍣ㄧ殑鐘舵€併€?/li>
    4. 璋冪敤 it.hasNext() 鐢ㄤ簬妫€娴嬮泦鍚堜腑鏄惁杩樻湁鍏冪礌銆?/li>

     

     

    璁╄凯浠e櫒 it 閫愪釜杩斿洖鎵€鏈夊厓绱犳渶绠€鍗曠殑鏂规硶鏄娇鐢?while 寰幆锛?/p>

     

    1.  
      val it = Iterator("Baidu", "Google", "Tencent", "Taobao")
    2.  
       
    3.  
      while (it.hasNext){
    4.  
      println(it.next())
    5.  
      }

     

    鎵撳嵃缁撴灉涓猴細

     

    1.  
      Baidu
    2.  
      Google
    3.  
      Tencent
    4.  
      Taobao

     

    鏌ユ壘鏈€澶т笌鏈€灏忓厓绱?/h3>

    浣犲彲浠ヤ娇鐢?nbsp;it.min 鍜?nbsp;it.max 鏂规硶浠庤凯浠e櫒涓煡鎵炬渶澶т笌鏈€灏忓厓绱狅紝瀹炰緥濡備笅:

     

    1.  
      val ita = Iterator(20, 40, 2, 50, 69, 90)
    2.  
      val itb = Iterator(20, 40, 2, 50, 69, 90)
    3.  
       
    4.  
      println("鏈€澶у厓绱犳槸锛? + ita.max)
    5.  
      println("鏈€灏忓厓绱犳槸锛? + itb.min)

     

    鎵ц缁撴灉涓猴細

     

    1.  
      鏈€澶у厓绱犳槸锛?span class="hljs-number">90
    2.  
      鏈€灏忓厓绱犳槸锛?span class="hljs-number">2



    鑾峰彇杩唬鍣ㄧ殑闀垮害

    浣犲彲浠ヤ娇鐢?nbsp;it.size 鎴?nbsp;it.length 鏂规硶鏉ユ煡鐪嬭凯浠e櫒涓殑鍏冪礌涓暟銆傚疄渚嬪涓嬶細

     

    1.  
      val ita = Iterator(20, 40, 2, 50, 69, 90)
    2.  
      val itb = Iterator(20, 40, 2, 50, 69, 90)
    3.  
       
    4.  
      println("ita.size : " + ita.size)
    5.  
      println("itb.length : " + itb.length)

     

    鎵撳嵃缁撴灉涓猴細

     

    1.  
      ita.size : 6
    2.  
      itb.length : 6



    Scala Iterator 甯哥敤鏂规硶

     

    • 鍙傝€冨簳閮ㄩ檮褰曪細

     

     

     

     

    闄勫綍锛?/h2>

    List甯哥敤鏂规硶

     鎶€鏈垎浜浘鐗? src=

     

     

    闃熷垪鍜屾爤甯哥敤鎿嶄綔

     

     

    Scala Set 甯哥敤鏂规硶

    鎶€鏈垎浜浘鐗? src= 

     

     

    Scala Map甯哥敤鏂规硶

    鎶€鏈垎浜浘鐗? src= 

     

     

    鍏冪粍甯哥敤鏂规硶

     

    Scala Option 甯哥敤鏂规硶

    鎶€鏈垎浜浘鐗? src= 

     

     

    Scala Iterator 甯哥敤鏂规硶

     鎶€鏈垎浜浘鐗? src=

     

  • 以上是关于Scala闆嗗悎绫诲瀷璇﹁В的主要内容,如果未能解决你的问题,请参考以下文章

    Java 闆嗗悎

    闆嗗悎ArrayList鍒嗘瀽

    python瀛︿範绡?5 - 瀛楀吀 闆嗗悎

    DOM-Text绫诲瀷銆丆omment绫诲瀷銆丆DATASection绫诲瀷銆丏ocumentType绫诲瀷銆丏ocumentFragment绫诲瀷銆丄ttr绫诲瀷

    js鏁扮粍鐨勫0鏄庝笌搴旂敤

    201771010142 寮犵嚂& 鏉ㄨ搲搴? 瀹為獙鍗佷竴 闆嗗悎