将val names = List(“Alice”, “Bob”, “Nick”)集合中首字母为’A’的筛选到新的集合。
object Test3 {
def main(args: Array[String]): Unit = {
val names = List("Alice", "Bob", "Nick")
var list: List[String] = names.filter(_.startsWith("A"))
list.foreach(v=>println(v))
}
}
将list集合中的元素去除包含abc 的元素并求出每个元素出现的次数
import scala.collection.immutable
object Test9 {
def main(args: Array[String]): Unit = {
var list =List("abc,hello,abcmouse,spark,flink,sqoop,spark")
//逗号切开存到另一个集合
val list1: List[String] = list.flatMap(_.split(","))
val listt: List[String] = list1.filter(!_.contains("abc"))
//存到元祖中
val list2: List[(String, Int)] = listt.map((_, 1))
//分组
val map: Map[String, List[(String, Int)]] = list2.groupBy(_._1)
//存入集合
val list3: immutable.Iterable[String] = map.map(t => t._1 + "个数:" + t._2.size)
list3.foreach(v=>println(v+" "))
}
}