import scala.collection.mutable 可变集合包
import scala.collection.immutable 不可变集合包,默认不可变
1、可变list
val list2 = ListBuffer[Int](1,2,3)
list2.append(4,5,6)
list2.foreach(println)
2、不可变list
val list = List[String]("hello scala","hello java","hello spark")
val list1 = list.flatMap(s=>{s.split(" ")})//flatMap一对多
// list1.foreach(println(_))
// val result = list.map(a=>a.split(" "))//map一对一关系
// result.foreach(arr=>{
// println("新的数组:")
// arr.foreach(println)})
//val list = List(1,2,3,4,"true")
// val list = List[Int](1,2,3)
//list.foreach(println(_))
// for(elem<-list){
// println(elem)
// }
3、可变长set集合—— mutable.Set
val set5 = mutable.Set[Int](1,2,3)
set5.+=(100)
set5.foreach(println(_))
4、set集合无序,并且不重复,默认不可变长
immutable.Set
val set = immutable.Set[Int](1,2,4,4,5)
val set1 = Set[Int](3,4,5,6)
val set2 = set.intersect(set1)//取交集set & set1 操作符操作
val set3 = set.diff(set1)//取差集set &` set1
// set3.foreach(println(_))
5、可变长度map mutable.MapString,Int
l map4 = mutable.Map[String,Int]()
map4.put("a",100)
map4.put("b",200)
map4.foreach(println(_))
6、不可变map
val map1 = Map[String,Int](("a",1),("b",2),("c",3),("d",4))
val map2 = Map[String,Int](("a",100),("b",2),("c",300),("e",4))
val stringToInt = map1.++(map2)
val stringToInt2 = map1.++:(map2)
//stringToInt2.foreach(println(_))
val map = Map[String,Int]("a"->100,"b"->200,("c",300),("c",400))
val keys = map.keys
keys.foreach(key=> {
val value = map.get(key).get
// println(s"key = $key,value = $value")
}
)
val value = map.get("aa").getOrElse("No value")
// println(value)
//println(map)//Map(a -> 100, b -> 200, c -> 400)
// for(elem<-map){
// println(elem)
// }
// map.foreach(println(_))
7、元组
/**
* 元组最多支持22个元素
* 元组可new,可不new,可直接写()中元素
*/
val tuple1 = new Tuple1("hello")
val tuple2 = new Tuple2("a",100)
val tuple3 = new Tuple3(1,true,'c')
val tuple4 = Tuple4(1,3.4,"abc",true)
val tuple22 = Tuple22(1,2,3,4,5,6,7,8,9,10,11,12,"abc",14,15,16,17,18,19,20,21,22)
//println(tuple22._13)
/**
* 遍历
*/
val iterator = tuple22.productIterator
iterator.foreach(println(_))
// while (iterator.hasNext){
// println(iterator.next())