前面整理过
,继续整理scala中集合常用操作使用方法,需要注意的是scala 中 Set Seq List Array等集合常用操作方法大致相同,但也有显著差别,本例是以List作为标准1.创建列表
scala> val days = List("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") days: List[String] = List(Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)
2.创建空列表
scala> val l = Nil l: scala.collection.immutable.Nil.type = List() scala> val l = List() l: List[Nothing] = List()
3.用字符串创建列表
scala> val l = "Hello" :: "Hi" :: "Hah" :: "WOW" :: "WOOW" :: Nil l: List[String] = List(Hello, Hi, Hah, WOW, WOOW)
4.用“:::”叠加创建新列表
scala> val wow = l ::: List("WOOOW", "WOOOOW") wow: List[String] = List(Hello, Hi, Hah, WOW, WOOW, WOOOW, WOOOOW)
5.通过索引获取列表值
scala> l(3) res0: String = WOW
6.获取值长度为3的元素数目
scala> l.count(s => s.length == 3) res1: Int = 2
7.返回去掉l头两个元素的新列表
scala> l.drop(2) res2: List[String] = List(Hah, WOW, WOOW) scala> l res3: List[String] = List(Hello, Hi, Hah, WOW, WOOW)
8.返回去掉l后两个元素的新列表
scala> l.dropRight(2) res5: List[String] = List(Hello, Hi, Hah) scala> l res6: List[String] = List(Hello, Hi, Hah, WOW, WOOW)
9.判断l是否存在某个元素
scala> l.exists(s => s == "Hah") res7: Boolean = true
10滤出长度为3的元素
scala> l.filter(s => s.length == 3) res8: List[String] = List(Hah, WOW)
11判断所有元素是否以“H”打头
scala> l.forall(s => s.startsWith("H")) res10: Boolean = false
12判断所有元素是否以“H”结尾
scala> l.forall(s => s.endsWith("W")) res11: Boolean = false
13打印每个元素
scala> l.foreach(s => print(s + ' ')) Hello Hi Hah WOW WOOW
14取出第一个元素
scala> l.head res17: String = Hello
15取出最后一个元素
scala> l.last res20: String = WOOW
16剔除最后一个元素,生成新列表
scala> l.init res18: List[String] = List(Hello, Hi, Hah, WOW)
17剔除第一个元素,生成新列表
scala> l.tail res49: List[String] = List(Hi, Hah, WOW, WOOW)
18判断列表是否为空
scala> l.isEmpty res19: Boolean = false
19获得列表长度
scala> l.length res21: Int = 5
20修改每个元素,再反转每个元素形成新列表
scala> l.map(s => {val s1 = s + " - 01"; s1.reverse}) res29: List[String] = List(10 - olleH, 10 - iH, 10 - haH, 10 - WOW, 10 - WOOW)
21生成用逗号隔开的字符串
scala> l.mkString(", ") res30: String = Hello, Hi, Hah, WOW, WOOW
22反序生成新列表
scala> l.reverse res41: List[String] = List(WOOW, WOW, Hah, Hi, Hello)
23按字母递增排序
scala> l.sortWith(_.compareTo(_) < 0) res48: List[String] = List(Hah, Hello, Hi, WOOW, WOW)
24 遍历集合并返回新集合
val res=combineFieldKeys.map(_.concat("MonDay"))