map<\/code> \u8f6c\u6362\u5143\u7d20\uff0c\u4e3b\u8981\u5e94\u7528\u4e8e\u4e0d\u53ef\u53d8\u96c6\u5408<\/p> (1 to 10).map(i => i * i)\n(1 to 10).flatMap(i => (1 to i).map(j => i * j))<\/code><\/pre><\/li> \n transform<\/code> \u4e0e map<\/code> \u76f8\u540c\uff0c\u4e0d\u8fc7\u7528\u4e8e\u53ef\u53d8\u96c6\u5408\uff0c\u76f4\u63a5\u8f6c\u6362<\/p> ArrayBuffer("Peter", "Paul", "Mary").transform(_.toUpperCase)<\/code><\/pre><\/li> \n collect<\/code> \u63a5\u6536\u504f\u51fd\u6570(PartialFunction<\/code>)\u4f5c\u4e3a\u53c2\u6570\uff1b\u6a21\u5f0f\u5339\u914d\u4e5f\u662f\u4e00\u79cd\u504f\u51fd\u6570<\/p> "-3+4".collect {\n case '+' => 1 ; \n case '-' => -1 \n} \/\/ Vector(-1, 1)<\/code><\/pre><\/li> \n groupBy<\/code> \u6309\u6307\u5b9a\u51fd\u6570\u5206\u7ec4\uff0c\u8fd4\u56de Map<\/code><\/p> val words = Array("Abc", "ab")\nval map = words.groupBy(_.substring(0, 1).toUpperCase)\n\/\/ Map(A -> Array(Abc, ab))<\/code><\/pre><\/li> \n <\/ul><\/li> \n \u63a5\u6536\u4e8c\u5143\u51fd\u6570 \n \n reduceLeft<\/code> \u4ece\u5de6\u5411\u53f3\u89c4\u7ea6 f(f(f(a, b), c), d)<\/code><\/li> \n <\/ul> List(1, 7, 2, 9).reduceLeft(_ - _)\n \/\/ ((1 - 7) - 2) - 9 = 1 - 7 - 2 - 9 = -17<\/code><\/pre> \n \n reduceRight<\/code> \u4ece\u53f3\u5411\u5de6\u89c4\u7ea6 f(a, f(b, f(c, d)))<\/code><\/p> List(1, 7, 2, 9).reduceRight(_ - _)\n\/\/ 1 - (7 - (2 - 9)) = 1 - 7 + 2 - 9 = -13<\/code><\/pre><\/li> \n foldLeft<\/code> \u63d0\u4f9b\u521d\u59cb\u503c+\u4e8c\u5143\u51fd\u6570\uff0c\u4ece\u5de6\u5411\u53f3\u6298\u53e0\uff0c\u6bcf\u6b21\u8ba1\u7b97\u7ed3\u679c\u5728\u5de6\u4fa7 \n \n - \u53ef\u7528
\/:<\/code>\uff08\u8868\u793a\u6811\u5f62\u5de6\u4fa7\uff09\u64cd\u4f5c\u7b26\u8868\u793a\uff0c(init \/: collection)(function)<\/code><\/li> \n <\/ul><\/li> \n foldRight<\/code> \u63d0\u4f9b\u521d\u59cb\u503c+\u4e8c\u5143\u51fd\u6570\uff0c\u4ece\u53f3\u5411\u5de6\u6298\u53e0\uff0c\u6bcf\u6b21\u8ba1\u7b97\u7ed3\u679c\u5728\u53f3\u4fa7 \n \n - \u53ef\u7528
:\\<\/code>\uff08\u8868\u793a\u6811\u5f62\u53f3\u4fa7\uff09\u64cd\u4f5c\u7b26\u8868\u793a\uff0c(collection :\\ init)(function)<\/code><\/li> \n <\/ul> List(1, 7, 2, 9).foldLeft(0)(_ - _) \n(0 \/: List(1, 7, 2, 9))(_ - _)\n\/\/ 0 - 1 - 7 - 2 - 9 = -19<\/code><\/pre><\/li> \n scanLeft<\/code> \u548c scanRight<\/code> \u7ed3\u5408\u4e86 folding \u548c mapping\uff0c\u7ed3\u679c\u4e3a\u6240\u6709\u7684\u4e2d\u95f4\u8fc7\u7a0b\u503c<\/p> (1 to 10).scanLeft(0)(_ + _) \/\/ Vector(0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55)<\/code><\/pre><\/li> \n <\/ul><\/li> \n zip<\/code> \u62c9\u94fe\uff0c\u5373\u5c06\u4e24\u4e2a\u96c6\u5408\u5404\u4e2a\u5143\u7d20\u50cf\u62c9\u94fe\u4e00\u6837\u4ea4\u53c9\u7ed3\u5408\u5728\u4e00\u8d77<\/p> List(1,2,3) zip List("a","b","c") \/\/ List((1,a), (2,b), (3,c))<\/code><\/pre> \n \n - \u957f\u5ea6\u4e0d\u4e00\u81f4\u7684\u96c6\u5408\u5219\u4ee5\u8f83\u5c0f\u7684\u957f\u5ea6\u4e3a\u51c6<\/li> \n <\/ul><\/li> \n
zipAll<\/code> \u4e3a\u957f\u5ea6\u8f83\u77ed\u7684\u96c6\u5408\u8bbe\u7f6e\u9ed8\u8ba4\u503c\uff0c<\/p> this.zipAll(that, thisDefault, thatDefault)<\/code><\/pre><\/li> \n zipWithIndex<\/code> \u8fd4\u56de\u5143\u7d20\u53ca\u5bf9\u5e94\u7684\u4e0b\u6807<\/p> "Scala".zipWithIndex\n\/\/ Vector((S,0), (c,1), (a,2), (l,3), (a,4))<\/code><\/pre><\/li> \n view<\/code> \u4e3a\u96c6\u5408\u521b\u5efa\u5ef6\u8fdf\u89c6\u56fe
scala val lazyView = (1 to 1000000).view lazyView.take(100).last \/\/100<\/code> \n \n - \u5bf9\u89c6\u56fe\u7684\u64cd\u4f5c\u90fd\u4e0d\u4f1a\u7acb\u5373\u8ba1\u7b97\uff08\u5305\u62ec\u7b2c\u4e00\u4e2a\u5143\u7d20\u4e5f\u4e0d\u4f1a\uff09<\/li> \n
- \u4e0e
Stream<\/code> \u4e0d\u540c\uff0c\u4e0d\u4f1a\u7f13\u5b58\u4efb\u4f55\u503c<\/li> \n apply<\/code> \u65b9\u6cd5\u4f1a\u5f3a\u5236\u8ba1\u7b97\u6574\u4e2a\u89c6\u56fe\uff0c\u4f7f\u7528 lazyView.take(i).last<\/code> \u4ee3\u66ff lazyView(i)<\/code><\/li> \n <\/ul><\/li> \n par<\/code> \u5e76\u884c\u5316\u96c6\u5408\uff0c\u540e\u7eed\u5e94\u7528\u7684\u65b9\u6cd5\u90fd\u4f1a\u5e76\u53d1\u8ba1\u7b97
scala for (i <- (0 until 100).par) print(s" $i") \/\/ 1-99<\/code> \n \n - \u5f88\u597d\u7684\u89e3\u51b3\u5e76\u53d1\u7f16\u7a0b\u95ee\u9898<\/li> \n
- \u5c06\u96c6\u5408\u53d8\u4e3a\u5bf9\u4e8e\u7684\u5e76\u884c\u5316\u5b9e\u73b0<\/li> \n
- \u5bf9\u4e8e\u4ea7\u751f\u7684\u7ed3\u679c\uff0c\u4e0e\u4e32\u884c\u65b9\u5f0f\u7684\u7ed3\u679c\u4e00\u81f4 \uff08\u5982
for...yield...<\/code>\uff09<\/li> \n - \u53ef\u4f7f\u7528
seq<\/code>\uff0ctoArray<\/code> \u7b49\u65b9\u6cd5\u5c06\u96c6\u5408\u8fd8\u539f<\/li> \n - \u90e8\u5206\u65b9\u6cd5\u4e0d\u80fd\u5e76\u53d1\u64cd\u4f5c \n
\n - \u4f7f\u7528
reduce<\/code> \u66ff\u4ee3 reduceLeft<\/code>\uff0c\u5148\u5bf9\u5404\u90e8\u5206\u96c6\u5408\u64cd\u4f5c\uff0c\u7136\u540e\u805a\u5408\u7ed3\u679c\uff0c\u4f46\u64cd\u4f5c\u5fc5\u987b\u6ee1\u8db3\u7ed3\u5408\u5f8b<\/li> \n - \u4f7f\u7528
aggregate<\/code> \u66ff\u4ee3 foldLeft<\/code>\uff0c\u5148\u5bf9\u5404\u90e8\u5206\u96c6\u5408\u64cd\u4f5c\uff0c\u7136\u540e\u7528\u53e6\u4e00\u4e2a\u64cd\u4f5c\u5c06\u7ed3\u679c\u805a\u5408<\/li> \n <\/ul> str.par.aggregate(Set[Char]())(_ + _, _ ++ _) \/\/ \u7b49\u4ef7\u4e8e str.foldLeft(Set[Char]())(_ + _)<\/code><\/li> \n <\/ul><\/li> \n <\/ul> \n<\/div>","orderid":"0","title":"Scala Collection Method","smalltitle":"","mid":"0","fname":"Scala","special_id":"0","bak_id":"0","info":"0","hits":"201","pages":"1","comments":"0","posttime":"2019-10-09 20:01:16","list":"1570622476","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> Collection<\/A> Method<\/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":"14.17.22.32","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"Scala Collection Method","lastview":"1713459520","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}