Scala有一个非常通用,丰富,强大,可组合的集合库;集合是高阶的(high level)并暴露了一大套操作方法。
Scala的所有的集合类都可以在包 scala.collection 包中找到,其中集合类都是高级抽象类或特性
Iterable[T] 是所有可遍历的集合,它提供了迭代的方法(foreach)。Seq[T] 是有序集合,Set[T]是数学上的集合(无序且不重复),Map[T]是关联数组,也是无序的。
Scala 集合类系统地区分了可变的和不可变的集合。
可变集合可以在适当的地方被更新或扩展,意味着你可以修改、添加、移除一个集合的元素。
而不可变集合类,相比之下,永远不会改变。不过,你仍然可以模拟添加,移除或更新操作。但是这 些操作将在每一种情况下都返回一个新的集合,同时使原来的集合不发生改变。
可变的集合类位于 scala.collection.mutable 包中,而不可变的集合位于 scala.collection.immutable 。
scala.collection 包中的集合,既可以是可变的,也可以是不可变的
官方文档
http://www.scala-lang.org/docu/files/collections-api/collections.html
![a49dfa5cdaec24189c41525db5ee5113.png](https://i-blog.csdnimg.cn/blog_migrate/0b723e84d70301e893535e633926e4f6.jpeg)
List集合
创建输出
![7eeb7b417658c5b22971434a4b8547a8.png](https://i-blog.csdnimg.cn/blog_migrate/81798b4bd6bbda8bfca7e8aca8d4f6bc.jpeg)
获取头部元素
![df5f4d5d21d6da3d0b6081d1c1d84f7e.png](https://i-blog.csdnimg.cn/blog_migrate/f1ee5d667d711884858676228dbcba20.jpeg)
获取除去头部元素之外的元素
![52a3ec8ebdf4329b03fc1ffc2e02eb92.png](https://i-blog.csdnimg.cn/blog_migrate/e93fdbcfe8478465d11e79d22bef89f7.jpeg)
获取尾部元素
![90a8eca966ac4dbda672caa1f9e9d236.png](https://i-blog.csdnimg.cn/blog_migrate/5591898eb23df0c6416c6c39f4e389f2.jpeg)
获取除去尾部元素之外的元素
![17ad2aec39a8c707b0ba45c07c190be7.png](https://i-blog.csdnimg.cn/blog_migrate/f30bea1589efe1ad599bf3c917123b14.jpeg)
可以从头拼接的方式创建集合
![ccac80f86dd06f04f8d7cccee0274677.png](https://i-blog.csdnimg.cn/blog_migrate/b6f81424b3f37e57b535ece134042213.jpeg)
如果一直拼接创建集合(Nil可以理解为空串)
![7391f0d336539dcc0a51a2fc2ebb36c7.png](https://i-blog.csdnimg.cn/blog_migrate/e96e53d18fa269a2e85dd23dc66fbf76.jpeg)
如果从尾到头创建集合
![99de21ceb2725b6334b2b4e3cefe9e4e.png](https://i-blog.csdnimg.cn/blog_migrate/c86aff0d424fcd15140b1e4dddb8368f.jpeg)
遍历for
![51ad96bbfd470d457c87581e3f4aa6bd.png](https://i-blog.csdnimg.cn/blog_migrate/80edeb5640e8b7fc57a81b60a933bdc7.jpeg)
如果更改元素会提示错误list4(0) = 100;
![ced576b8ac338144fc82ef767c960154.png](https://i-blog.csdnimg.cn/blog_migrate/269f82b6163acac0bc5841ca98203598.jpeg)
可变List
import scala.collection.mutable.ListBuffer
构建
![41d49ff28a308c30a8f5da8e675bd744.png](https://i-blog.csdnimg.cn/blog_migrate/a04d865eb424bf7d18f9b1814b761926.jpeg)
添加元素,遍历
![944425b161c940b04d38a7c222378add.png](https://i-blog.csdnimg.cn/blog_migrate/ad160027db7301672a306be97043c0b0.jpeg)
给指定位置插入元素
![f92841fbf977e1ce10a585bb15350904.png](https://i-blog.csdnimg.cn/blog_migrate/2d38e9d10f942d01f21f95063e416d7d.jpeg)
删除元素
![fafd65ee16455091d9a52316f386795f.png](https://i-blog.csdnimg.cn/blog_migrate/da0523b8c1a5ff98432af6a68c057317.jpeg)
直接删除List
![6a71f5c1da6a1e4cfd899480551e53ab.png](https://i-blog.csdnimg.cn/blog_migrate/8007fc7358add08821a19a907b316410.jpeg)
删除指定位置的元素
![39c2c2a968a10aeac836ef635907b993.png](https://i-blog.csdnimg.cn/blog_migrate/30c0b2844c971ac28991a4d540681b94.jpeg)
获取数据
![bde642baf500d55a982d0b1064d5676d.png](https://i-blog.csdnimg.cn/blog_migrate/3ba2c69e527360889bf52d03c19a33f2.jpeg)
更新数据
![74d4709bc40c550ad5dd1110486cc775.png](https://i-blog.csdnimg.cn/blog_migrate/495f7948b2f27a60fcc4636ff448738a.jpeg)
转换
![3e1c11b15b17099aa576378e89c4731d.png](https://i-blog.csdnimg.cn/blog_migrate/914592bd3be539394da31cee904e65df.jpeg)
如果想用Java集合
![1e0bc140bc5b11321b1f26bf0468c152.png](https://i-blog.csdnimg.cn/blog_migrate/c362070c8da81d989bbe80172fa2dfe8.jpeg)
把Java中List转换为scala中List
import scala.collection.JavaConverters._
![58bea112e22bb166bde5882445c739a4.png](https://i-blog.csdnimg.cn/blog_migrate/a0ff356b15837cff66f40fe4c5eb2afd.jpeg)
List中常见方法:
reverse
![c7feb52307b03302768878cba016c749.png](https://i-blog.csdnimg.cn/blog_migrate/5a089fda589a356221685845e790bbff.jpeg)
Zip方法
![50e55239dc582cf316239d2369014ba9.png](https://i-blog.csdnimg.cn/blog_migrate/65193a6a1a5b9507b85eecd5e6c9a91a.jpeg)
ZipAll
![7f26a7f25e45b7edff9266b317bab00f.png](https://i-blog.csdnimg.cn/blog_migrate/3189af93be0f4ec34eb298f6b3c1941e.jpeg)
转换range
![0d44cc4eb82e0844837eb1fb37aec529.png](https://i-blog.csdnimg.cn/blog_migrate/cc1405e607304401ad25055e9b797333.jpeg)
Foreach遍历,Foreach一般用于数据输出,因为foreach没有返回值
![745ebdaa583fc604bbe01baa783b48da.png](https://i-blog.csdnimg.cn/blog_migrate/e5200295a8dc29ff094d9a161f7d2a0d.jpeg)
转换为字符串
![8d796fe09d0bb20900fd08bd788afc0f.png](https://i-blog.csdnimg.cn/blog_migrate/43fd11db05e38a65698d4ec2f6cc1698.jpeg)
Map输出
![1e63fb198fb928f86630f9b04e138305.png](https://i-blog.csdnimg.cn/blog_migrate/cf7e22241c0a614df37eca93001e2820.jpeg)
Map一般是对每一个元素进行处理的,是有返回值的
![80959b8269298462b5373293f3311bd4.png](https://i-blog.csdnimg.cn/blog_migrate/0eaad5beb790e597c49054d1f5fad9b6.jpeg)
flatMap 对集合中每一个元素进行处理,使用给定函数,并且返回一个新的集合 是在map的基础上进行了一次扁平化操作,不是把函数的返回值作为新集合的元素,而是把返回值中的元素作为新集合的元素
![44d0361993f6909ecad79e538426c5b3.png](https://i-blog.csdnimg.cn/blog_migrate/6cc7e3718d76834dbda6b5ffc4fec997.jpeg)
Filter数据过滤 对集合中的每个元素调用给定函数,如果函数执行返回值是true 就会保留,否则反之
![9da68025c01b315eb9ac0c1642ed61e5.png](https://i-blog.csdnimg.cn/blog_migrate/21ed0763cb7a348b3065c41c616d4428.jpeg)
FilterNot和上面相反
![e0b4178c74eebc7ba5858440d67e0b21.png](https://i-blog.csdnimg.cn/blog_migrate/81e53f6e9dbc46fb77d34e8450cfe509.jpeg)
Reduce 数据聚合
![01509228901dd782b5b3ae8e4e89e802.png](https://i-blog.csdnimg.cn/blog_migrate/ea1917bf883b884372b11c9dd107b798.jpeg)
Fold 数据聚合 和reduce区别是可以给一个初始值
![8e55866cb71906cfa8d1285bab9b7183.png](https://i-blog.csdnimg.cn/blog_migrate/c7aa1884997165cca115517fba7b6a68.jpeg)
FoldLeft 给定的初始值可以是任意类型
![6307555799241af0f67e5b40e7a28fdf.png](https://i-blog.csdnimg.cn/blog_migrate/7066f1b6f448a0d2418aadae87abc90d.jpeg)
FoldRight
![4987308795418a9e3eef8efafb8c1e77.png](https://i-blog.csdnimg.cn/blog_migrate/e5f3912a5f1a1d7454057fd48c63bc90.jpeg)
Sorted 对集合元素进行排序
![face9b304b7a6df39462608a668ef653.png](https://i-blog.csdnimg.cn/blog_migrate/7793eae6039cf49a8577c0c3736f2cfe.jpeg)
sortBy 类似Sorted 区别在于sortBy根据函数的返回值排序
![7120afa4e7f442226f54bbd7513a981d.png](https://i-blog.csdnimg.cn/blog_migrate/d696e989706d9a3117dee14932987395.jpeg)
groupBy 按照给定的函数的返回值对数据进行分组操作,返回结果是Map集合
![6e2e3d7be574e0590a18cf0f648a985d.png](https://i-blog.csdnimg.cn/blog_migrate/dad24bcb86899f0a5aaa471133b0e908.jpeg)
Set
1)无序不重复
2)+/++/-/-- 都会创造一个新的Set
3)+=/++=/-=/--= 不会创造一个新的Set
创建输出
![f294c1f592e47a5cac4bdd6ec254769d.png](https://i-blog.csdnimg.cn/blog_migrate/454f0405f30c5faa05a02febfb6175c3.jpeg)
添加元素
![89cd4523d31daa0a7004e66ab0bc79f7.png](https://i-blog.csdnimg.cn/blog_migrate/a8d9fffe551963e17213b51c27854d34.jpeg)
++添加
![1df433eb63498a15a6cd01ee54ea362d.png](https://i-blog.csdnimg.cn/blog_migrate/d0746d25c9390cc44f9089ed4086d5ef.jpeg)
-正好相反
![16cf06cc701cd0814eaa4169a509cc00.png](https://i-blog.csdnimg.cn/blog_migrate/9ab8155921347d1fbfcbf92a36faf7d6.jpeg)
Set的常用方法和List相似,可以作为练习网上查询使用
访问元素 Set中没有专门的方式去访问数据,因为是无序的
如果直接使用set(0) 是判断0是否存在于set中
![63ad88a8ad08c8f2364cdbde067b8ef4.png](https://i-blog.csdnimg.cn/blog_migrate/649481bcaa2d2af7cbab0411fe379ba1.jpeg)
将List转换为Set,完成去重
![8089525472d484d1f31f3c71bce29e3d.png](https://i-blog.csdnimg.cn/blog_migrate/1c71584b15c0abfafd77867ed9a50811.jpeg)
可变Set
import scala.collection.mutable._
![e1b171cd95396c4bd53b6465f850902c.png](https://i-blog.csdnimg.cn/blog_migrate/9c55987b97679165e371b0e40f10f18c.jpeg)
有一个特殊的地方:一种特殊的添加元素的方式
![eaddebdf1f9e8efacc2a7b29eef8b1c3.png](https://i-blog.csdnimg.cn/blog_migrate/131973ec01594d045d359a1c727d0a96.jpeg)
Map
是一种Key-Value键值对集合
创建Map
![7664e7940c1c1d7fdc87d12d42e0a932.png](https://i-blog.csdnimg.cn/blog_migrate/9d50a4f2ae7ac0fa9905f9028c5656a6.jpeg)
数据读取
![05730c96925edd97c44ec48d596c9fc3.png](https://i-blog.csdnimg.cn/blog_migrate/fc76f0cdbc0e296880959bba21f29e3d.jpeg)
Get方法
![7fed669b4da40f4b6748e81ef8bcd85a.png](https://i-blog.csdnimg.cn/blog_migrate/4497723d1d352f3b504394e001c0250c.jpeg)
GetOrElse
![fcfddd6f1910a951410c75816638662e.png](https://i-blog.csdnimg.cn/blog_migrate/4076c3abf047e8536914f357340d82a7.jpeg)
可变Map
import scala.collection.mutable._
![ca68a02c4088fe58f2be2c3761129866.png](https://i-blog.csdnimg.cn/blog_migrate/ec4a8d2c398d2ffb2412f956552b9fa3.jpeg)
添加元素
![27a2bf7f0b1eb1dfefb54d70b6164e38.png](https://i-blog.csdnimg.cn/blog_migrate/3589cf195bae5a2816608c8847711f2c.jpeg)
更新操作,如果存在就更新,不存在就插入
![029285f3e42728b0a83d6dc11c0078ae.png](https://i-blog.csdnimg.cn/blog_migrate/966d5f91b7e0361c7b1c9f98a267373e.jpeg)
遍历
![1ffc5093fc38173c547cdc5e305e9550.png](https://i-blog.csdnimg.cn/blog_migrate/6f9c20dfc287fa398184297ccdaa0a5e.jpeg)
也可以直接key-value
![6b884e5d9e50d58c45f7b9ee6ff6456a.png](https://i-blog.csdnimg.cn/blog_migrate/d06fb36c63179b7f9d3e7b105be23a5a.jpeg)
其它用到时候再看
集合常用高阶函数
![ccd8e825cfe6f7feabc885c4c2e5701b.png](https://i-blog.csdnimg.cn/blog_migrate/c5678a8f51a65345eee909d54ba6f4bd.jpeg)