{"rsdb":{"rid":"351964","subhead":"","postdate":"0","aid":"249825","fid":"90","uid":"1","topic":"1","content":"
\n

Scala\u7684\u96c6\u5408\u4f53\u7cfb\u7ed3\u6784<\/span><\/strong><\/h3> \n

 <\/p> \n

\/\/ Scala\u4e2d\u7684\u96c6\u5408\u4f53\u7cfb\u4e3b\u8981\u5305\u62ec\uff1a<\/span>Iterable<\/span>\u3001<\/span>Seq<\/span>\u3001<\/span>Set<\/span>\u3001<\/span>Map<\/span>\u3002\u5176\u4e2d<\/span>Iterable<\/span>\u662f\u6240\u6709\u96c6\u5408<\/span>trait<\/span>\u7684\u6839<\/span>trai<\/span>\u3002\u8fd9\u4e2a\u7ed3\u6784\u4e0e<\/span>Java<\/span>\u7684\u96c6\u5408\u4f53\u7cfb\u975e\u5e38\u76f8\u4f3c\u3002<\/span><\/p> \n

 <\/p> \n

\/\/ Scala\u4e2d\u7684\u96c6\u5408\u662f\u5206\u6210\u53ef\u53d8\u548c\u4e0d\u53ef\u53d8\u4e24\u7c7b\u96c6\u5408\u7684\uff0c\u5176\u4e2d\u53ef\u53d8\u96c6\u5408\u5c31\u662f\u8bf4\uff0c\u96c6\u5408\u7684\u5143\u7d20\u53ef\u4ee5\u52a8\u6001\u4fee\u6539\uff0c\u800c\u4e0d\u53ef\u53d8\u96c6\u5408\u7684\u5143\u7d20\u5728\u521d\u59cb\u5316\u4e4b\u540e\uff0c\u5c31\u65e0\u6cd5\u4fee\u6539\u4e86\u3002\u5206\u522b\u5bf9\u5e94<\/span>scala.collection.mutable<\/span>\u548c<\/span>scala.collection.immutable<\/span>\u4e24\u4e2a\u5305\u3002<\/span><\/p> \n

 <\/p> \n

\/\/ Seq\u4e0b\u5305\u542b\u4e86<\/span>Range<\/span>\u3001<\/span>ArrayBuffer<\/span>\u3001<\/span>List<\/span>\u7b49\u5b50<\/span>trait<\/span>\u3002\u5176\u4e2d<\/span>Range<\/span>\u5c31\u4ee3\u8868\u4e86\u4e00\u4e2a\u5e8f\u5217\uff0c\u901a\u5e38\u53ef\u4ee5\u4f7f\u7528\u201c<\/span>1 to 10<\/span>\u201d\u8fd9\u79cd\u8bed\u6cd5\u6765\u4ea7\u751f\u4e00\u4e2a<\/span>Range<\/span>\u3002 <\/span>ArrayBuffer<\/span>\u5c31\u7c7b\u4f3c\u4e8e<\/span>Java<\/span>\u4e2d\u7684<\/span>ArrayList<\/span>\u3002<\/span><\/p> \n

 <\/p> \n

 <\/p> \n

List<\/strong><\/h3> \n

\/\/ List\u4ee3\u8868\u4e00\u4e2a\u4e0d\u53ef\u53d8\u7684\u5217\u8868<\/span><\/p> \n

\/\/ List\u7684\u521b\u5efa\uff0c<\/span>val list = List(1, 2, 3, 4)<\/span><\/p> \n

\/\/ List\u6709<\/span>head<\/span>\u548c<\/span>tail<\/span>\uff0c<\/span>head<\/span>\u4ee3\u8868<\/span>List<\/span>\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c<\/span>tail<\/span>\u4ee3\u8868\u7b2c\u4e00\u4e2a\u5143\u7d20\u4e4b\u540e\u7684\u6240\u6709\u5143\u7d20\uff0c<\/span>list.head<\/span>\uff0c<\/span>list.tail<\/span><\/p> \n

\/\/ List\u6709\u7279\u6b8a\u7684<\/span>::<\/span>\u64cd\u4f5c\u7b26\uff0c\u53ef\u4ee5\u7528\u4e8e\u5c06<\/span>head<\/span>\u548c<\/span>tail<\/span>\u5408\u5e76\u6210\u4e00\u4e2a<\/span>List<\/span>\uff0c<\/span>0 :: list<\/span><\/p> \n

\/\/ ::\u8fd9\u79cd\u64cd\u4f5c\u7b26\u8981\u6e05\u695a\uff0c\u5728<\/span>spark<\/span>\u6e90\u7801\u4e2d\u90fd\u662f\u6709\u4f53\u73b0\u7684\uff0c\u4e00\u5b9a\u8981\u80fd\u591f\u770b\u61c2\uff01<\/span><\/p> \n

\/\/ \u5982\u679c\u4e00\u4e2a<\/span>List<\/span>\u53ea\u6709\u4e00\u4e2a\u5143\u7d20\uff0c\u90a3\u4e48\u5b83\u7684<\/span>head<\/span>\u5c31\u662f\u8fd9\u4e2a\u5143\u7d20\uff0c\u5b83\u7684<\/span>tail<\/span>\u662f<\/span>Nil<\/span><\/p> \n

 <\/p> \n

\/\/ \u6848\u4f8b\uff1a\u7528\u9012\u5f52\u51fd\u6570\u6765\u7ed9<\/span>List<\/span>\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u52a0\u4e0a\u6307\u5b9a\u524d\u7f00\uff0c\u5e76\u6253\u5370\u52a0\u4e0a\u524d\u7f00\u7684\u5143\u7d20<\/span><\/p> \n

def decorator(l: List[Int], prefix: String) {<\/p> \n

  if (l != Nil) {<\/p> \n

    println(prefix + l.head)<\/p> \n

    decorator(l.tail, prefix)<\/p> \n

  }<\/p> \n

}<\/p> \n

 <\/p> \n

 <\/p> \n

LinkedList<\/strong><\/h3> \n

 <\/p> \n

\/\/ LinkedList\u4ee3\u8868\u4e00\u4e2a\u53ef\u53d8\u7684\u5217\u8868\uff0c\u4f7f\u7528<\/span>elem<\/span>\u53ef\u4ee5\u5f15\u7528\u5176\u5934\u90e8\uff0c\u4f7f\u7528<\/span>next<\/span>\u53ef\u4ee5\u5f15\u7528\u5176\u5c3e\u90e8<\/span><\/p> \n

\/\/ val l = scala.collection.mutable.LinkedList(1, 2, 3, 4, 5); l.elem; l.next<\/p> \n

 <\/p> \n

\/\/ \u6848\u4f8b\uff1a\u4f7f\u7528<\/span>while<\/span>\u5faa\u73af\u5c06<\/span>LinkedList<\/span>\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u4e58\u4ee5<\/span>2<\/span><\/p> \n

val list = scala.collection.mutable.LinkedList(1, 2, 3, 4, 5)<\/p> \n

var currentList = list<\/p> \n

while (currentList != Nil) {<\/p> \n

  currentList.elem = currentList.elem * 2<\/p> \n

  currentList = currentList.next<\/p> \n

}<\/p> \n

 <\/p> \n

\/\/ \u6848\u4f8b\uff1a\u4f7f\u7528<\/span>while<\/span>\u5faa\u73af\u5c06<\/span>LinkedList<\/span>\u4e2d\uff0c\u4ece\u7b2c\u4e00\u4e2a\u5143\u7d20\u5f00\u59cb\uff0c\u6bcf\u9694\u4e00\u4e2a\u5143\u7d20\uff0c\u4e58\u4ee5<\/span>2<\/span><\/p> \n

val list = scala.collection.mutable.LinkedList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)<\/p> \n

var currentList = list<\/p> \n

var first = true<\/p> \n

while (currentList != Nil && currentList.next != Nil) {<\/p> \n

  if (first) { currentList.elem = currentList.elem * 2; first = false }<\/p> \n

  currentList  = currentList.next.next<\/p> \n

  if (currentList != Nil) currentList.elem = currentList.elem * 2<\/p> \n

}<\/p> \n

 <\/p> \n

Set<\/strong><\/h3> \n

\/\/Set \u4ee3\u8868\u4e00\u4e2a\u6ca1\u6709\u91cd\u590d\u5143\u7d20\u7684\u96c6\u5408<\/span><\/p> \n

\/\/ \u5c06\u91cd\u590d\u5143\u7d20\u52a0\u5165<\/span>Set<\/span>\u662f\u6ca1\u6709\u7528\u7684\uff0c\u6bd4\u5982<\/span>val s = Set(1, 2, 3); s + 1; s + 4<\/span><\/p> \n

\/\/ \u800c\u4e14<\/span>Set<\/span>\u662f\u4e0d\u4fdd\u8bc1\u63d2\u5165\u987a\u5e8f\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c<\/span>Set<\/span>\u4e2d\u7684\u5143\u7d20\u662f\u4e71\u5e8f\u7684\uff0c<\/span>val s = new scala.collection.mutable.HashSet[Int](); s += 1; s += 2; s += 5<\/span><\/p> \n

 <\/p> \n

\/\/ LinkedHashSet\u4f1a\u7528\u4e00\u4e2a\u94fe\u8868\u7ef4\u62a4\u63d2\u5165\u987a\u5e8f\uff0c<\/span>val s = new scala.collection.mutable.LinkedHashSet[Int](); i += 1; s += 2; s += 5<\/span><\/p> \n

 <\/p> \n

\/\/ SrotedSet\u4f1a\u81ea\u52a8\u6839\u636e<\/span>key<\/span>\u6765\u8fdb\u884c\u6392\u5e8f\uff0c<\/span>val s = scala.collection.mutable.SortedSet("orange", "apple", "banana")<\/span><\/p> \n

 <\/p> \n

 <\/p> \n

\u96c6\u5408\u7684\u51fd\u6570\u5f0f\u7f16\u7a0b<\/strong><\/h3> \n

\/\/ \u96c6\u5408\u7684\u51fd\u6570\u5f0f\u7f16\u7a0b\u975e\u5e38\u975e\u5e38\u975e\u5e38\u4e4b\u91cd\u8981\uff01\uff01\uff01<\/span><\/p> \n

\/\/ \u5fc5\u987b\u5b8c\u5168\u638c\u63e1\u548c\u7406\u89e3<\/span>Scala<\/span>\u7684\u9ad8\u9636\u51fd\u6570\u662f\u4ec0\u4e48\u610f\u601d\uff0c<\/span>Scala<\/span>\u7684\u96c6\u5408\u7c7b\u7684<\/span>map<\/span>\u3001<\/span>flatMap<\/span>\u3001<\/span>reduce<\/span>\u3001<\/span>reduceLeft<\/span>\u3001<\/span>foreach<\/span>\u7b49\u8fd9\u4e9b\u51fd\u6570\uff0c\u5c31\u662f\u9ad8\u9636\u51fd\u6570\uff0c\u56e0\u4e3a\u53ef\u4ee5\u63a5\u6536\u5176\u4ed6\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570<\/span><\/p> \n

\/\/ \u9ad8\u9636\u51fd\u6570\u7684\u4f7f\u7528\uff0c\u4e5f\u662f<\/span>Scala<\/span>\u4e0e<\/span>Java<\/span>\u6700\u5927\u7684\u4e00\u70b9\u4e0d\u540c\uff01\uff01\uff01\u56e0\u4e3a<\/span>Java<\/span>\u91cc\u9762\u662f\u6ca1\u6709\u51fd\u6570\u5f0f\u7f16\u7a0b\u7684\uff0c\u4e5f\u80af\u5b9a\u6ca1\u6709\u9ad8\u9636\u51fd\u6570\uff0c\u4e5f\u80af\u5b9a\u65e0\u6cd5\u76f4\u63a5\u5c06\u51fd\u6570\u4f20\u5165\u4e00\u4e2a\u65b9\u6cd5\uff0c\u6216\u8005\u8ba9\u4e00\u4e2a\u65b9\u6cd5\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570<\/span><\/p> \n

\/\/ \u5bf9<\/span>Scala<\/span>\u9ad8\u9636\u51fd\u6570\u7684\u7406\u89e3\u3001\u638c\u63e1\u548c\u4f7f\u7528\uff0c\u53ef\u4ee5\u5927\u5927\u589e\u5f3a\u4f60\u7684\u6280\u672f\uff0c\u800c\u4e14\u4e5f\u662f<\/span>Scala<\/span>\u6700\u6709\u8bf1\u60d1\u529b\u3001\u6700\u6709\u4f18\u52bf\u7684\u4e00\u4e2a\u529f\u80fd\uff01\uff01\uff01<\/span><\/p> \n

\/\/ \u6b64\u5916\uff0c\u5728<\/span>Spark<\/span>\u6e90\u7801\u4e2d\uff0c\u6709\u5927\u91cf\u7684\u51fd\u6570\u5f0f\u7f16\u7a0b\uff0c\u4ee5\u53ca\u57fa\u4e8e\u96c6\u5408\u7684\u9ad8\u9636\u51fd\u6570\u7684\u4f7f\u7528\uff01\uff01\uff01\u6240\u4ee5\u5fc5\u987b\u638c\u63e1\uff0c\u624d\u80fd\u770b\u61c2<\/span>spark<\/span>\u6e90\u7801<\/span><\/p> \n

 <\/p> \n

\/\/ map\u6848\u4f8b\u5b9e\u6218\uff1a\u4e3a<\/span>List<\/span>\u4e2d\u6bcf\u4e2a\u5143\u7d20\u90fd\u6dfb\u52a0\u4e00\u4e2a\u524d\u7f00<\/span><\/p> \n

List("Leo", "Jen", "Peter", "Jack").map("name is " + _)<\/p> \n

 <\/p> \n

\/\/ faltMap\u6848\u4f8b\u5b9e\u6218\uff1a\u5c06<\/span>List<\/span>\u4e2d\u7684\u591a\u884c\u53e5\u5b50\u62c6\u5206\u6210\u5355\u8bcd<\/span><\/p> \n

List("Hello World", "You Me").flatMap(_.split(" "))<\/p> \n

 <\/p> \n

\/\/ foreach\u6848\u4f8b\u5b9e\u6218\uff1a\u6253\u5370<\/span>List<\/span>\u4e2d\u7684\u6bcf\u4e2a\u5355\u8bcd<\/span><\/p> \n

List("I", "have", "a", "beautiful", "house").foreach(println(_))<\/p> \n

 <\/p> \n

\/\/ zip\u6848\u4f8b\u5b9e\u6218\uff1a\u5bf9\u5b66\u751f\u59d3\u540d\u548c\u5b66\u751f\u6210\u7ee9\u8fdb\u884c\u5173\u8054<\/span><\/p> \n

List("Leo", "Jen", "Peter", "Jack").zip(List(100, 90, 75, 83))<\/p> \n

 <\/p> \n

 <\/p> \n

 <\/p> \n

\u51fd\u6570\u5f0f\u7f16\u7a0b\u7efc\u5408\u6848\u4f8b\uff1a\u7edf\u8ba1\u591a\u4e2a\u6587\u672c\u5185\u7684\u5355\u8bcd\u603b\u6570<\/strong><\/h3> \n

\/\/ \u4f7f\u7528<\/span>scala<\/span>\u7684<\/span>io<\/span>\u5305\u5c06\u6587\u672c\u6587\u4ef6\u5185","orderid":"0","title":"scala \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e4b\u96c6\u5408\u64cd\u4f5c(\u4e00)","smalltitle":"","mid":"0","fname":"Scala","special_id":"0","bak_id":"0","info":"0","hits":"222","pages":"2","comments":"0","posttime":"2019-09-03 03:46:49","list":"1567453609","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"scala<\/A> \u51fd\u6570<\/A> \u7f16\u7a0b<\/A> \u96c6\u5408<\/A> \u64cd\u4f5c<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"120.229.33.54","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"scala \u51fd\u6570\u5f0f\u7f16\u7a0b\u4e4b\u96c6\u5408\u64cd\u4f5c","lastview":"1713959522","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}