Scala闆嗗悎绫诲瀷璇﹁В
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Scala闆嗗悎绫诲瀷璇﹁В相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%9f%a5%e7%9c%8b' title='鏌ョ湅'>鏌ョ湅
鏁村瀷 ++ first 涓嶅悓 head lisScala闆嗗悎
Scala鎻愪緵浜嗕竴濂楀緢濂界殑闆嗗悎瀹炵幇锛屾彁渚涗簡涓€浜涢泦鍚堢被鍨嬬殑鎶借薄銆?/p>
Scala 闆嗗悎鍒嗕负鍙彉鐨勫拰涓嶅彲鍙樼殑闆嗗悎銆?/p>
鍙彉闆嗗悎鍙互鍦ㄩ€傚綋鐨勫湴鏂硅鏇存柊鎴栨墿灞曘€傝繖鎰忓懗鐫€浣犲彲浠ヤ慨鏀癸紝娣诲姞锛岀Щ闄や竴涓泦鍚堢殑鍏冪礌銆傝€屼笉鍙彉闆嗗悎绫绘案杩滀笉浼氭敼鍙樸€備笉杩囷紝浣犱粛鐒跺彲浠ユā鎷熸坊鍔狅紝绉婚櫎鎴栨洿鏂版搷浣溿€備絾鏄繖浜涙搷浣滃皢鍦ㄦ瘡涓€绉嶆儏鍐典笅閮借繑鍥炰竴涓柊鐨勯泦鍚堬紝鍚屾椂浣垮師鏉ョ殑闆嗗悎涓嶅彂鐢熸敼鍙樸€?/p>
闆嗗悎涓熀鏈粨鏋勶細
鍑犵甯哥敤闆嗗悎绫诲瀷绀轰緥锛?/p>
-
// 瀹氫箟鏁村瀷 List
-
val x = List(1, 2, 3, 4)
-
-
// 瀹氫箟 Set
-
var x = Set(1, 3, 5, 7)
-
-
// 瀹氫箟 Map
-
val x = Map("one" -> 1, "two" -> 2, "three" -> 3)
-
-
// 鍒涘缓涓や釜涓嶅悓绫诲瀷鍏冪礌鐨勫厓缁?/span>
-
val x = (10, "Runoob")
-
-
// 瀹氫箟 Option
-
val x: Option[Int] = Some(5)
Scala List(鍒楄〃)
Scala 鍒楄〃绫讳技浜庢暟缁勶紝瀹冧滑鎵€鏈夊厓绱犵殑绫诲瀷閮界浉鍚岋紝浣嗘槸瀹冧滑涔熸湁鎵€涓嶅悓锛氬垪琛ㄦ槸涓嶅彲鍙樼殑锛屽€间竴鏃﹁瀹氫箟浜嗗氨涓嶈兘鏀瑰彉锛屽叾娆″垪琛?鍏锋湁閫掑綊鐨勭粨鏋勶紙涔熷氨鏄摼鎺ヨ〃缁撴瀯锛夎€屾暟缁勪笉鏄€?/p>
鍒楄〃鐨勫厓绱犵被鍨?T 鍙互鍐欐垚 List[T]銆備緥濡傦紝浠ヤ笅鍒楀嚭浜嗗绉嶇被鍨嬬殑鍒楄〃锛?/p>
-
// 瀛楃涓插垪琛?/span>
-
val site: List[String] = List("Sina", "Google", "Baidu")
-
-
// 鏁村瀷鍒楄〃
-
val nums: List[Int] = List(1, 2, 3, 4)
-
-
// 绌哄垪琛?/span>
-
val empty: List[Nothing] = List()
-
-
// 浜岀淮鍒楄〃
-
val dim: List[List[Int]] =
-
List(
-
List(1, 0, 0),
-
List(0, 1, 0),
-
List(0, 0, 1)
-
)
鏋勯€犲垪琛ㄧ殑涓や釜鍩烘湰鍗曚綅鏄?nbsp;Nil 鍜?nbsp;::
Nil 涔熷彲浠ヨ〃绀轰负涓€涓┖鍒楄〃銆?/p>
浠ヤ笂瀹炰緥鎴戜滑鍙互鍐欐垚濡備笅鎵€绀猴細锛?:锛夌鍙疯繛鎺ラ『搴忔槸浠庡彸鍒板乏
-
// 瀛楃涓插垪琛?/span>
-
val site = "Sina" :: ("Google" :: ("Baidu" :: Nil))
-
-
// 鏁村瀷鍒楄〃
-
val nums = 1 :: (2 :: (3 :: (4 :: Nil)))
-
-
// 绌哄垪琛?/span>
-
val empty = Nil
-
-
// 浜岀淮鍒楄〃
-
val dim = (1 :: (0 :: (0 :: Nil))) ::
-
(0 :: (1 :: (0 :: Nil))) ::
-
(0 :: (0 :: (1 :: Nil))) :: Nil
鍩烘湰鎿嶄綔
Scala鍒楄〃鏈変笁涓熀鏈搷浣滐細
- head 杩斿洖鍒楄〃绗竴涓厓绱?/li>
- tail 杩斿洖涓€涓垪琛紝鍖呭惈闄や簡绗竴鍏冪礌涔嬪鐨勫叾浠栧厓绱狅細锛堟槸涓€涓垪琛ㄦ垨鑰匩il锛?/li>
- isEmpty 鍦ㄥ垪琛ㄤ负绌烘椂杩斿洖true
瀵逛簬Scala鍒楄〃鐨勪换浣曟搷浣滈兘鍙互浣跨敤杩欎笁涓熀鏈搷浣滄潵琛ㄨ揪銆備唬鐮佺ず渚嬪涓?
-
val site = "Sina" :: ("Google" :: ("Baidu" :: Nil))
-
val nums = Nil
-
//鎵撳嵃娴嬭瘯head銆乼ail銆乮sEmpty鍔熻兘
-
println(site.head)
-
println(site.tail)
-
println(site.isEmpty)
-
println(nums.isEmpty)
鎵撳嵃缁撴灉涓猴細
-
Sina
-
List(Google, Baidu)
-
false
-
true
杩炴帴鍒楄〃
浣犲彲浠ヤ娇鐢?nbsp; ::: 銆?nbsp;List.:::() 鎴?nbsp; List.concat() 鏂规硶鏉ヨ繛鎺ヤ袱涓垨澶氫釜鍒楄〃銆?/p>
浠g爜绀轰緥濡備笅:
-
val site1 = "Sina" :: ("Google" :: ("Baidu" :: Nil))
-
val site2 = "Facebook" :: ("Taobao" :: Nil)
-
-
// 浣跨敤 ::: 杩愮畻绗?/span>
-
var combine = site1 ::: site2
-
println( "site1 ::: site2 : " + combine )
-
-
// 浣跨敤 list.:::() 鏂规硶锛屾敞鎰忥細缁撴灉涓簊ite2鍒楄〃鍏冪礌鍦ㄥ墠锛宻ite1鍦ㄥ悗
-
combine = site1.:::(site2)
-
println( "site1.:::(site2) : " + combine )
-
-
// 浣跨敤 concat 鏂规硶
-
combine = List.concat(site1, site2)
-
println( "List.concat(site1, site2) : " + combine )
-
}
鎵撳嵃缁撴灉锛?/p>
-
site1 ::: site2 : List(Sina, Google, Baidu, Facebook, Taobao)
-
site1.:::(site2) : List(Facebook, Taobao, Sina, Google, Baidu)
-
List.concat(site1, site2) : List(Sina, Google, Baidu, Facebook, Taobao)
List.fill()
鎴戜滑鍙互浣跨敤 List.fill() 鏂规硶鏉ュ垱寤轰竴涓寚瀹氶噸澶嶆暟閲忕殑鍏冪礌鍒楄〃锛?/p>
-
val site = List.fill(3)("Baidu") // 閲嶅 Runoob 3娆?/span>
-
println( "site : " + site )
-
-
val num = List.fill(10)(2) // 閲嶅鍏冪礌 2, 10 娆?/span>
-
println( "num : " + num )
鎵撳嵃缁撴灉涓猴細
-
site : List(Baidu, Baidu, Baidu)
-
num : List(2, 2, 2, 2, 2, 2, 2, 2, 2, 2)
List.tabulate()
List.tabulate() 鏂规硶鏄€氳繃缁欏畾鐨勫嚱鏁版潵鍒涘缓鍒楄〃銆?/p>
鏂规硶鐨勭涓€涓弬鏁颁负鍏冪礌鐨勬暟閲忥紝鍙互鏄簩缁寸殑锛岀浜屼釜鍙傛暟涓烘寚瀹氱殑鍑芥暟锛屾垜浠€氳繃鎸囧畾鐨勫嚱鏁拌绠楃粨鏋滃苟杩斿洖鍊兼彃鍏ュ埌鍒楄〃涓紝璧峰鍊间负 0锛屽疄渚嬪涓嬶細
-
// 閫氳繃缁欏畾鐨勫嚱鏁板垱寤?5 涓厓绱?/span>
-
val squares = List.tabulate(6)(n => n * n)
-
println( "涓€缁?: " + squares )
-
-
// 鍒涘缓浜岀淮鍒楄〃
-
val mul = List.tabulate( 4,5 )( _ * _ )
-
println( "浜岀淮 : " + mul )
鎵撳嵃缁撴灉涓猴細
-
涓€缁?: List(0, 1, 4, 9, 16, 25)
-
浜岀淮 : 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 鐢ㄤ簬灏嗗垪琛ㄧ殑椤哄簭鍙嶈浆锛屽疄渚嬪涓嬶細
-
val site = "Sina" :: ("Google" :: ("Baidu" :: Nil))
-
println( "鍙嶈浆鍓?: " + site )
-
println( "鍙嶈浆鍓?: " + site.reverse )
鎵撳嵃缁撴灉涓猴細
-
鍙嶈浆鍓?: List(Sina, Google, Baidu)
-
鍙嶈浆鍓?: List(Baidu, Google, Sina)
鍒楄〃缂撳瓨(ListBuffer)
List绫昏兘澶熸彁渚涘鍒楄〃澶撮儴锛岃€岄潪灏鹃儴鐨勫揩閫熻闂€傚鏋滈渶瑕佸悜缁撳熬娣诲姞瀵硅薄锛屽垯闇€瑕佸厛瀵硅〃澶村墠缂€鍏冪礌鏂瑰紡鍙嶅悜鏋勯€犲垪琛紝瀹屾垚涔嬪悗鍐嶈皟鐢╮everse銆?/p>
涓婅堪闂鍙︿竴绉嶈В鍐虫柟寮忓氨鏄娇鐢↙istBuffer锛岃繖鍙互閬垮厤reverse鎿嶄綔銆侺istBuffer鏄彲鍙樺璞★紝瀹冨彲浠ユ洿楂樻晥鐨勯€氳繃娣诲姞鍏冪礌鏉ユ瀯寤哄垪琛ㄣ€?/p>
浣跨敤ListBuffer鏇夸唬List鍙︿竴涓悊鐢辨槸閬垮厤鏍堟孩鍑洪闄┿€?/p>
ListBuffer浣跨敤绀轰緥锛?/p>
-
val buf: ListBuffer[Int] = new ListBuffer[Int]
-
//寰€鍚庢坊鍔?/span>
-
buf += 1
-
buf += 2
-
-
//鍓嶇紑娣诲姞
-
val buf2 = 3 +: buf
-
println(buf2.toString())
-
-
//ListBuffer杞琇ist
-
println(buf2.toList.toString())
List甯哥敤鏂规硶
- 鍙傝€冨簳閮ㄩ檮褰?
Scala闃熷垪鍜屾爤
闃熷垪
濡傛灉浣犻渶瑕佸厛杩涘厛鍑哄簭鍒楋紝浣犲彲浠ヤ娇鐢≦ueue(闃熷垪)銆係cala闆嗗悎鎻愪緵浜嗗彲鍙樺拰涓嶅彲鍙樼殑Queue銆?/p>
涓嶅彲鍙楺ueue浠g爜绀轰緥锛?/p>
-
//浣跨敤浼寸敓瀵硅薄鍒涘缓涓€涓猶ueue
-
val que = Queue[Int]()
-
-
//浣跨敤enqueue涓轰笉鍙彉闃熷垪娣诲姞鍏冪礌
-
val que1 = que.enqueue(1)
-
-
//寰€闃熷垪娣诲姞澶氫釜鍏冪礌锛屾妸闆嗗悎浣滀负enqueue鐨勫弬鏁?/span>
-
val que2 = que1.enqueue(List(2,3,4,5))
-
-
//浠庨槦鍒楀ご閮ㄧЩ闄ゅ厓绱狅紝浣跨敤dequeue
-
//绗竴涓弬鏁颁负澶撮儴绉婚櫎鐨勫厓绱狅紝绗簩涓弬鏁颁负鍓╀笅鐨勯槦鍒?/span>
-
val (elem1,que3) = que2.dequeue
-
-
//鎵撳嵃绉婚櫎鐨勫厓绱?/span>
-
println(elem1)
-
-
//鎵撳嵃鍓╀笅鐨勯槦鍒?/span>
-
println(que3)
鎵撳嵃缁撴灉涓猴細
-
1
-
Queue(2, 3, 4, 5)
鍙彉Queue浠g爜绀轰緥锛?/p>
-
//浣跨敤浼寸敓瀵硅薄鍒涘缓涓€涓彲鍙榪ueue
-
var que = scala.collection.mutable.Queue[String]()
-
-
//浣跨敤 += 绗﹀彿娣诲姞鍗曚釜鍏冪礌
-
que += "A"
-
-
//浣跨敤 ++= 绗﹀彿娣诲姞澶氫釜鍏冪礌
-
que ++= List("B","C","D")
-
-
//浣跨敤dequeue绉婚櫎澶撮儴鍏冪礌
-
val a = que.dequeue
-
-
//鎵撳嵃绉婚櫎鐨勫厓绱?/span>
-
println(a)
-
-
//鎵撳嵃闃熷垪涓墿涓嬬殑鍏冪礌
-
print(que)
鎵撳嵃缁撴灉锛?/p>
-
A
-
Queue(B, C, D)
鏍?/h3>
濡傛灉闇€瑕佺殑鏄悗杩涘厛鍑猴紝浣犲彲浠ヤ娇鐢⊿tack锛屽畠鍚屾牱鍦⊿cala鐨勯泦鍚堜腑鏈夊彲鍙樺拰涓嶅彲鍙樼増鏈€傚厓绱犵殑鎺ㄥ叆浣跨敤push锛屽脊鍑虹敤pop锛屽彧鑾峰彇鏍堥《鍏冪礌鑰屼笉绉婚櫎鍙互浣跨敤top銆?/p>
鍙彉鏍堢ず渚嬶細
-
//浣跨敤Stack绫荤殑浼寸敓瀵硅薄鍒涘缓Stack瀵硅薄
-
var stack = scala.collection.mutable.Stack[Int]()
-
-
//寰€鏍坰tack涓帇濡傚厓绱?/span>
-
stack.push(1)
-
stack.push(2)
-
stack.push(3)
-
-
//鎵撳嵃鏌ョ湅鏍堝唴鍏冪礌
-
println(stack)
-
-
//鑾峰彇鏍堥《鍏冪礌鐨勫€?/span>
-
val tval = stack.top
-
println("鏍堥《鍏冪礌涓?: " + tval)
-
-
//绉婚櫎鏍堥《鍏冪礌
-
val pval = stack.pop()
-
println("绉婚櫎鐨勬爤椤跺厓绱犱负 : " + pval)
-
-
//鎵撳嵃绉婚櫎鏍堥《鍏冪礌鍚庯紝鍓╀笅鐨勬爤鍐呭厓绱?/span>
-
println(stack)
鎵撳嵃缁撴灉锛?/p>
-
Stack(3, 2, 1)
-
鏍堥《鍏冪礌涓?: 3
-
绉婚櫎鐨勬爤椤跺厓绱犱负 : 3
-
Stack(2, 1)
闃熷垪鍜屾爤甯哥敤鎿嶄綔
- 鍙傝€冨簳閮ㄩ檮褰曪細
Scala Set(闆?
- Scala Set(闆?鏄病鏈夐噸澶嶇殑瀵硅薄闆嗗悎锛屾墍鏈夌殑鍏冪礌閮芥槸鍞竴鐨勩€?/li>
- Scala 闆嗗悎鍒嗕负鍙彉鐨勫拰涓嶅彲鍙樼殑闆嗗悎銆?/li>
- 榛樿鎯呭喌涓嬶紝Scala 浣跨敤鐨勬槸涓嶅彲鍙橀泦鍚堬紝濡傛灉鎯充娇鐢ㄥ彲鍙橀泦鍚堬紝闇€寮曠敤 scala.collection.mutable.Set 鍖呫€?/li>
- 榛樿寮曠敤 scala.collection.immutable.Set銆?/li>
涓嶅彲鍙橀泦鍚堝疄渚嬪涓嬶細
-
val set = Set(1,2,3)
-
println(set.getClass.getName) //
-
-
println(set.exists(_ % 2 == 0)) //true
-
println(set.drop(1)) //Set(2,3)
鎵撳嵃缁撴灉涓猴細
-
scala.collection.immutable.Set$Set3
-
true
-
Set(2, 3)
濡傛灉闇€瑕佷娇鐢ㄥ彲鍙橀泦鍚堥渶瑕佸紩鍏?scala.collection.mutable.Set锛?/p>
-
import scala.collection.mutable.Set // 鍙互鍦ㄤ换浣曞湴鏂瑰紩鍏?鍙彉闆嗗悎
-
-
val mutableSet = Set(1,2,3)
-
println(mutableSet.getClass.getName) // scala.collection.mutable.HashSet
-
//寰€闆嗗悎鍐呮坊鍔犲厓绱?
-
mutableSet.add(4)
-
//鍒犻櫎鍊间负1鐨勫厓绱?/span>
-
mutableSet.remove(1)
-
//娣诲姞鍏冪礌5
-
mutableSet += 5
-
//鍒犻櫎鍊间负3鐨勫厓绱?/span>
-
mutableSet -= 3
-
-
println(mutableSet) // Set(5, 3, 4)
-
-
val another = mutableSet.toSet
-
println(another.getClass.getName) // scala.collection.immutable.Set
娉ㄦ剰锛?nbsp;铏界劧鍙彉Set鍜屼笉鍙彉Set閮芥湁娣诲姞鎴栧垹闄ゅ厓绱犵殑鎿嶄綔锛屼絾鏄湁涓€涓潪甯稿ぇ鐨勫樊鍒€傚涓嶅彲鍙楽et杩涜鎿嶄綔锛屼細浜х敓涓€涓柊鐨剆et锛屽師鏉ョ殑set骞舵病鏈夋敼鍙橈紝杩欎笌List涓€鏍枫€?鑰屽鍙彉Set杩涜鎿嶄綔锛屾敼鍙樼殑鏄Set鏈韩锛屼笌ListBuffer绫讳技銆?/p>
Set闆嗗悎鍩烘湰鎿嶄綔
Scala Set闆嗗悎鏈変笁涓熀鏈搷浣滐細
- head 杩斿洖闆嗗悎绗竴涓厓绱?/li>
- tail 杩斿洖涓€涓泦鍚堬紝鍖呭惈闄や簡绗竴鍏冪礌涔嬪鐨勫叾浠栧厓绱?/li>
- isEmpty 鍦ㄩ泦鍚堜负绌烘椂杩斿洖true
瀵逛簬Scala闆嗗悎鐨勪换浣曟搷浣滈兘鍙互浣跨敤杩欎笁涓熀鏈搷浣滄潵琛ㄨ揪銆?/p>
浠g爜绀轰緥濡備笅:
-
val site = Set("Sina", "Google", "Baidu")
-
val nums: Set[Int] = Set()
-
-
println( "head : " + site.head )
-
println( "tail : " + site.tail )
-
println( "isEmpty : " + site.isEmpty )
-
println( "isEmpty : " + nums.isEmpty )
鎵撳嵃缁撴灉涓猴細
-
head : Sina
-
tail : Set(Google, Baidu)
-
isEmpty : false
-
isEmpty : true
杩炴帴闆嗗悎
浣犲彲浠ヤ娇鐢?nbsp;++ 杩愮畻绗︽垨 Set.++() 鏂规硶鏉ヨ繛鎺ヤ袱涓泦鍚堛€傚鏋滃厓绱犳湁閲嶅鐨勫氨浼氱Щ闄ら噸澶嶇殑鍏冪礌銆傚疄渚嬪涓嬶細
-
val site1 = Set("Sina", "Google", "Baidu")
-
val site2 = Set("Faceboook", "Taobao")
-
-
// ++ 浣滀负杩愮畻绗︿娇鐢?/span>
-
var site = site1 ++ site2
-
println( "site1 ++ site2 : " + site )
-
-
// ++ 浣滀负鏂规硶浣跨敤
-
site = site1.++(site2)
-
println( "site1.++(site2) : " + site )
鎵撳嵃缁撴灉涓?
-
site1 ++ site2 : Set(Faceboook, Taobao, Sina, Google, Baidu)
-
site1.++(site2) : Set(Faceboook, Taobao, Sina, Google, Baidu)
鏌ユ壘闆嗗悎涓渶澶т笌鏈€灏忓厓绱?/h3>
浣犲彲浠ヤ娇鐢?nbsp;Set.min 鏂规硶鏉ユ煡鎵鹃泦鍚堜腑鐨勬渶灏忓厓绱狅紝浣跨敤 Set.max 鏂规硶鏌ユ壘闆嗗悎涓殑鏈€澶у厓绱犮€傚疄渚嬪涓嬶細
-
val num = Set(5,6,9,20,30,45)
-
-
// 鏌ユ壘闆嗗悎涓渶澶т笌鏈€灏忓厓绱?/span>
-
println( "Set(5,6,9,20,30,45) 鏈€灏忓厓绱犳槸 : " + num.min )
-
println( "Set(5,6,9,20,30,45) 鏈€澶у厓绱犳槸 : " + num.max )
鎵撳嵃缁撴灉涓猴細
-
Set(5,6,9,20,30,45) 鏈€灏忓厓绱犳槸 : 5
-
Set(5,6,9,20,30,45) 鏈€澶у厓绱犳槸 : 45
浜ら泦
浣犲彲浠ヤ娇鐢?nbsp;Set.& 鏂规硶鎴?nbsp;Set.intersect 鏂规硶鏉ユ煡鐪嬩袱涓泦鍚堢殑浜ら泦鍏冪礌銆傚疄渚嬪涓嬶細
-
val num1 = Set(5,6,9,20,30,45)
-
val num2 = Set(50,60,9,20,35,55)
-
-
// 浜ら泦
-
println( "num1.&(num2) : " + num1.&(num2) )
-
println( "num1.intersect(num2) : " + num1.intersect(num2) )
鎵撳嵃缁撴灉涓猴細
-
num1.&(num2) : Set(20, 9)
-
num1.intersect(num2) : Set(20, 9)
Scala Set 甯哥敤鏂规硶
- 鍙傝€冨簳閮ㄩ檮褰曪細
Scala Map(鏄犲皠)
- Map(鏄犲皠)鏄竴绉嶅彲杩唬鐨勯敭鍊煎锛坘ey/value锛夌粨鏋勩€?/li>
- 鎵€鏈夌殑鍊奸兘鍙互閫氳繃閿潵鑾峰彇銆?/li>
- Map 涓殑閿兘鏄敮涓€鐨勩€?/li>
- Map 涔熷彨鍝堝笇琛紙Hash tables锛夈€?/li>
- Map 鏈変袱绉嶇被鍨嬶紝鍙彉涓庝笉鍙彉锛屽尯鍒湪浜庡彲鍙樺璞″彲浠ヤ慨鏀瑰畠锛岃€屼笉鍙彉瀵硅薄涓嶅彲浠ャ€?/li>
- 榛樿鎯呭喌涓?Scala 浣跨敤涓嶅彲鍙?Map銆傚鏋滀綘闇€瑕佷娇鐢ㄥ彲鍙橀泦鍚堬紝浣犻渶瑕佹樉寮忕殑寮曞叆 import scala.collection.mutable.Map 绫?/li>
- 鍦?Scala 涓?浣犲彲浠ュ悓鏃朵娇鐢ㄥ彲鍙樹笌涓嶅彲鍙?Map锛屼笉鍙彉鐨勭洿鎺ヤ娇鐢?Map锛屽彲鍙樼殑浣跨敤 mutable.Map銆?/li>
浠ヤ笅瀹炰緥婕旂ず浜嗕笉鍙彉 Map 鐨勫簲鐢細
-
// 绌哄搱甯岃〃锛岄敭涓哄瓧绗︿覆锛屽€间负鏁村瀷
-
var A:Map[Char,Int] = Map()
-
-
// Map 閿€煎婕旂ず
-
val lang= Map("Java" -> "Oracle", "C#" -> "Microsoft")
-
鎴栬€?/div>val lang= Map(("Java","Oracle"), ("C#" , "Microsoft"))
瀹氫箟 Map 鏃讹紝闇€瑕佷负閿€煎瀹氫箟绫诲瀷銆傚鏋滈渶瑕佹坊鍔?key-value 瀵癸紝鍙互浣跨敤 + 鍙凤紝濡備笅鎵€绀猴細
A += (鈥榯鈥?->10 )
Map 鍩烘湰鎿嶄綔
Scala Map 鍑犵鍩烘湰鎿嶄綔锛歬eys銆乿alues銆乮sEmpty銆佽祴鍊硷紙鍙彉鏄犲皠锛?/p>
浠g爜绀轰緥锛?/p>
Keys:
-
val lang = Map("Java" -> "Oracle",
-
"C#" -> "Microsoft",
-
"Swift" -> "Apple")
-
Values:
-
val nums: Map[Int, Int] = Map()
-
println( "lang 涓殑閿负 : " + lang.keys )
-
println( "lang 涓殑鍊间负 : " + lang.values )
isEmpty:
-
println( "lang 鏄惁涓虹┖ : " + lang.isEmpty )
-
println( "nums 鏄惁涓虹┖ : " + nums.isEmpty )
keys鍜宨sEmpty鐨勬墦鍗扮粨鏋滀负锛?/p>
-
lang 涓殑閿负 : Set(Java, C#, Swift)
-
lang 涓殑鍊间负 : MapLike(Oracle, Microsoft, Apple)
-
lang 鏄惁涓虹┖ : false
-
nums 鏄惁涓虹┖ : true
璧嬪€硷細
-
var lang= scala.collection.mutable.Map("Java" -> "Oracle", "C#" -> "Microsoft")
-
lang("Java") = "sun"
-
println(lang)
鎵撳嵃缁撴灉涓猴細
Map(C# -> Microsoft, Java -> sun)
Map 鍚堝苟
浣犲彲浠ヤ娇鐢?nbsp;++ 杩愮畻绗︽垨 Map.++() 鏂规硶鏉ヨ繛鎺ヤ袱涓?Map锛孧ap 鍚堝苟鏃朵細绉婚櫎閲嶅鐨?key銆備互涓嬫紨绀轰簡涓や釜 Map 鍚堝苟鐨勫疄渚?
-
val lang =Map("Java" -> "Oracle",
-
"C#" -> "Microsoft",
-
"Swift" -> "Apple")
-
val color = Map("blue" -> "#0033FF",
-
"yellow" -> "#FFFF00",
-
"red" -> "#FF0000")
-
-
// ++ 浣滀负杩愮畻绗?/span>
-
var colors = lang ++ color
-
println( "lang ++ colors : " + colors )
-
-
// ++ 浣滀负鏂规硶
-
colors = lang.++(colors)
-
println( "lang.++(colors)) : " + colors )
鎵撳嵃缁撴灉涓猴細
-
lang ++ colors : Map(blue -> #0033FF, C# -> Microsoft, yellow -> #FFFF00, Java -> Oracle, red -> #FF0000, Swift -> Apple)
-
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>
-
val lang =Map("Java" -> "Oracle",
-
"C#" -> "Microsoft",
-
"Swift" -> "Apple")
-
-
lang.keys.foreach{ i =>
-
print( "Key = " + i )
-
println(" Value = " + lang(i) )}
鎵撳嵃缁撴灉涓猴細
-
Key = JavaValue = Oracle
-
Key = C#Value = Microsoft
-
Key = SwiftValue = Apple
鏌ョ湅 Map 涓槸鍚﹀瓨鍦ㄦ寚瀹氱殑 Key
浣犲彲浠ヤ娇鐢?nbsp;Map.contains 鏂规硶鏉ユ煡鐪?Map 涓槸鍚﹀瓨鍦ㄦ寚瀹氱殑 Key銆傚疄渚嬪涓嬶細
-
val lang =Map("Java" -> "Oracle",
-
"C#" -> "Microsoft",
-
"Swift" -> "Apple")
-
-
if(lang.contains("Swift")) {
-
println(lang("Swift")+"鍒涢€犱簡 Swift 璇█")
-
}
鎵撳嵃缁撴灉涓猴細
Apple鍒涢€犱簡 Swift 璇█
杩唬鏄犲皠
-
val lang = Map("Java" -> "Oracle", "C#" -> "Microsoft")
-
for ((k, v) <- lang) {
-
println(k + " : " + v)
-
}
鎵撳嵃缁撴灉涓猴細
-
Java : Oracle
-
C# : Microsoft
鏄犲皠涓璌锛孷鍙嶈浆
-
val lang = Map("Java" -> "Oracle", "C#" -> "Microsoft")
-
val lang2 = for ((k, v) <- lang) yield (v, k)
-
for ((k, v) <- lang2) {
-
println(k + " : " + v)
-
}
鎵撳嵃缁撴灉涓猴細
-
Oracle : Java
-
Microsoft : C#
宸叉帓搴忔槧灏?/h3>
-
//鎸夌収key鐨勫瓧鍏搁『搴忔帓搴忋€?/span>
-
val scores = scala.collection.immutable.SortedMap(("Bob", 8), ("Alice", 21), ("Fred", 17), ("Cindy", 15))
-
scores.foreach(person => println(person._1 + " : " + person._2))
鎵撳嵃缁撴灉涓猴細
-
Alice : 21
-
Bob : 8
-
Cindy : 15
-
Fred : 17
Scala Map甯哥敤鏂规硶
- 鍙傝€冨簳閮ㄩ檮褰曪細
Scala Tuple(鍏冪粍)
涓庡垪琛ㄤ竴鏍凤紝鍏冪粍涔熸槸涓嶅彲鍙樼殑锛屼絾涓庡垪琛ㄤ笉鍚岀殑鏄厓缁勫彲浠ュ寘鍚笉鍚岀被鍨嬬殑鍏冪礌銆?/p>
鍏冪粍鐨勫€兼槸閫氳繃灏嗗崟涓殑鍊煎寘鍚湪鍦嗘嫭鍙蜂腑鏋勬垚鐨勩€備緥濡傦細
-
//鍏冪粍涓畾涔変簡涓変釜鍏冪礌锛屽搴旂殑绫诲瀷鍒嗗埆涓篬Int, Double, java.lang.String]銆?/span>
-
val tuple1 = (1, 5.20, "Spark")
-
//鎴栬€?/span>
-
val tuple2 = new Tuple3(1,5.20,"Spark")
鎴戜滑鍙互浣跨敤 tuple1._1 璁块棶绗竴涓厓绱狅紝 tuple1._2 璁块棶绗簩涓厓绱狅紝濡備笅鎵€绀猴細
-
//鍏冪粍涓畾涔変簡涓変釜鍏冪礌锛屽搴旂殑绫诲瀷鍒嗗埆涓篬Int, Double, java.lang.String]銆?/span>
-
val tuple1 = (1, 5.20, "Spark")
-
-
println(tuple1._1 + " : " + tuple1._2 + " : " + tuple1._3)
鎵撳嵃缁撴灉涓猴細
1 : 5.2 : Spark
杩唬鍏冪粍
浣犲彲浠ヤ娇鐢?nbsp;Tuple.productIterator() 鏂规硶鏉ヨ凯浠h緭鍑哄厓缁勭殑鎵€鏈夊厓绱狅細
-
//鍏冪粍涓畾涔変簡涓変釜鍏冪礌锛屽搴旂殑绫诲瀷鍒嗗埆涓篬Int, Double, java.lang.String]銆?/span>
-
val tuple1 = (1, 5.20, "Spark")
-
-
tuple1.productIterator.foreach{i => println("value : " + i)}
鎵撳嵃缁撴灉涓猴細
-
value : 1
-
value : 5.2
-
value : Spark
鍏冪粍杞负瀛楃涓?/h3>
浣犲彲浠ヤ娇鐢?nbsp;Tuple.toString() 鏂规硶灏嗗厓缁勭殑鎵€鏈夊厓绱犵粍鍚堟垚涓€涓瓧绗︿覆锛屽疄渚嬪涓嬶細
-
//鍏冪粍涓畾涔変簡涓変釜鍏冪礌锛屽搴旂殑绫诲瀷鍒嗗埆涓篬Int, Double, java.lang.String]銆?/span>
-
val tuple1 = (1, 5.20, "Spark")
-
-
println(tuple1.toString())
鎵撳嵃缁撴灉涓猴細
(1,5.2,Spark)
鍏冪礌浜ゆ崲
浣犲彲浠ヤ娇鐢?nbsp;Tuple.swap 鏂规硶鏉ヤ氦鎹㈠厓缁勭殑鍏冪礌銆傚涓嬪疄渚嬶細
-
//鍏冪粍涓畾涔変簡涓や釜鍏冪礌锛屽搴旂殑绫诲瀷鍒嗗埆涓篬Int, java.lang.String]銆?/span>
-
val tuple1 = (1, "Spark")
-
//娉ㄦ剰锛歴wap鍑芥暟鍙兘鐢ㄤ簬涓や釜鍏冪礌鍏冪粍
-
println(tuple1.swap)
鎵撳嵃缁撴灉涓猴細
(Spark,1)
浣跨敤妯″紡鍖归厤鑾峰彇鍏冪粍
浠g爜绀轰緥锛?/p>
-
val t = (1, 3.14, "Fred")
-
val (first, second, third) = t
-
println(first + " : " + second + " : " + third)
鎵撳嵃缁撴灉锛?/p>
1 : 3.14 : Fred
鎷夐摼鎿嶄綔
-
val num = Array(1, 2, 3)
-
val str = Array("first", "second", "third")
-
val com = num.zip(str).toMap
-
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>
-
val myMap: Map[String, String] = Map("key1" -> "value")
-
val value1: Option[String] = myMap.get("key1")
-
val value2: Option[String] = myMap.get("key2")
-
-
println(value1) // Some("value1")
-
println(value2) // None
鎵撳嵃缁撴灉涓猴細
-
Some(value)
-
None
Option 鏈変袱涓瓙绫诲埆锛屼竴涓槸 Some锛屼竴涓槸 None锛屽綋浠栧洖浼?Some 鐨勬椂鍊欙紝浠h〃杩欎釜鍑藉紡鎴愬姛鍦扮粰浜嗕綘涓€涓?String锛岃€屼綘鍙互閫忚繃 get() 杩欎釜鍑藉紡鎷垮埌閭d釜 String锛屽鏋滀粬杩斿洖鐨勬槸 None锛屽垯浠h〃娌℃湁瀛楃涓插彲浠ョ粰浣犮€?/p>
閫氳繃妯″紡鍖归厤鏉ヨ緭鍑哄尮閰嶅€笺€?/p>
浠g爜绀轰緥锛?/p>
-
val lang =Map("Java" -> "Oracle",
-
"C#" -> "Microsoft",
-
"Swift" -> "Apple")
-
-
println(show(lang.get("Swift")))
-
println(show(lang.get("Scala")))
-
-
def show(x:Option[String]) = x match {
-
case Some(s) => s
-
case None => "?"
-
}
鎵撳嵃缁撴灉涓猴細
-
Apple
-
?
getOrElse() 鏂规硶
浣犲彲浠ヤ娇鐢?getOrElse() 鏂规硶鏉ヨ幏鍙栧厓缁勪腑瀛樺湪鐨勫厓绱犳垨鑰呬娇鐢ㄥ叾榛樿鐨勫€硷紝瀹炰緥濡備笅锛?/p>
-
val a:Option[Int] = Some(5)
-
val b:Option[Int] = Some(7)
-
val c:Option[Int] = None
-
-
println("a.getOrElse(0): " + a.getOrElse(0) )
-
println("b.getOrElse(1): " + b.getOrElse(1) )
-
println("c.getOrElse(10): " + c.getOrElse(10))
鎵撳嵃缁撴灉涓猴細
-
a.getOrElse(0): 5
-
b.getOrElse(1): 7
-
c.getOrElse(10): 10
isEmpty() 鏂规硶
浣犲彲浠ヤ娇鐢?isEmpty() 鏂规硶鏉ユ娴嬪厓缁勪腑鐨勫厓绱犳槸鍚︿负 None锛屽疄渚嬪涓嬶細
-
val a:Option[Int] = Some(5)
-
val b:Option[Int] = None
-
println("a.isEmpty: " + a.isEmpty )
-
println("b.isEmpty: " + b.isEmpty )
鎵撳嵃缁撴灉涓猴細
-
a.isEmpty: false
-
b.isEmpty: true
Scala Option 甯哥敤鏂规硶
- 鍙傝€冨簳閮ㄩ檮褰曪細
Scala Iterator(杩唬鍣?
- Scala Iterator锛堣凯浠e櫒锛変笉鏄竴涓泦鍚堬紝瀹冩槸涓€绉嶇敤浜庤闂泦鍚堢殑鏂规硶銆?/li>
- 杩唬鍣?it 鐨勪袱涓熀鏈搷浣滄槸 next 鍜?nbsp;hasNext銆?/li>
- 璋冪敤 it.next() 浼氳繑鍥炶凯浠e櫒鐨勪笅涓€涓厓绱狅紝骞朵笖鏇存柊杩唬鍣ㄧ殑鐘舵€併€?/li>
- 璋冪敤 it.hasNext() 鐢ㄤ簬妫€娴嬮泦鍚堜腑鏄惁杩樻湁鍏冪礌銆?/li>
璁╄凯浠e櫒 it 閫愪釜杩斿洖鎵€鏈夊厓绱犳渶绠€鍗曠殑鏂规硶鏄娇鐢?while 寰幆锛?/p>
-
val it = Iterator("Baidu", "Google", "Tencent", "Taobao")
-
-
while (it.hasNext){
-
println(it.next())
-
}
鎵撳嵃缁撴灉涓猴細
-
Baidu
-
Google
-
Tencent
-
Taobao
鏌ユ壘鏈€澶т笌鏈€灏忓厓绱?/h3>
浣犲彲浠ヤ娇鐢?nbsp;it.min 鍜?nbsp;it.max 鏂规硶浠庤凯浠e櫒涓煡鎵炬渶澶т笌鏈€灏忓厓绱狅紝瀹炰緥濡備笅:
-
val ita = Iterator(20, 40, 2, 50, 69, 90)
-
val itb = Iterator(20, 40, 2, 50, 69, 90)
-
-
println("鏈€澶у厓绱犳槸锛? + ita.max)
-
println("鏈€灏忓厓绱犳槸锛? + itb.min)
鎵ц缁撴灉涓猴細
-
鏈€澶у厓绱犳槸锛?span class="hljs-number">90
-
鏈€灏忓厓绱犳槸锛?span class="hljs-number">2
鑾峰彇杩唬鍣ㄧ殑闀垮害
浣犲彲浠ヤ娇鐢?nbsp;it.size 鎴?nbsp;it.length 鏂规硶鏉ユ煡鐪嬭凯浠e櫒涓殑鍏冪礌涓暟銆傚疄渚嬪涓嬶細
-
val ita = Iterator(20, 40, 2, 50, 69, 90)
-
val itb = Iterator(20, 40, 2, 50, 69, 90)
-
-
println("ita.size : " + ita.size)
-
println("itb.length : " + itb.length)
鎵撳嵃缁撴灉涓猴細
-
ita.size : 6
-
itb.length : 6
Scala Iterator 甯哥敤鏂规硶
- 鍙傝€冨簳閮ㄩ檮褰曪細
闄勫綍锛?/h2>
List甯哥敤鏂规硶
闃熷垪鍜屾爤甯哥敤鎿嶄綔
Scala Set 甯哥敤鏂规硶
Scala Map甯哥敤鏂规硶
鍏冪粍甯哥敤鏂规硶
Scala Option 甯哥敤鏂规硶
Scala Iterator 甯哥敤鏂规硶
以上是关于Scala闆嗗悎绫诲瀷璇﹁В的主要内容,如果未能解决你的问题,请参考以下文章
DOM-Text绫诲瀷銆丆omment绫诲瀷銆丆DATASection绫诲瀷銆丏ocumentType绫诲瀷銆丏ocumentFragment绫诲瀷銆丄ttr绫诲瀷