05.scala集合

数组

不可变数组:

object ArrayApp extends App { 
val a = new Array[String](5) 
println(a.length) 
a(1) = "hello" foreachList(a) 
println() 
val b = Array("hadoop","spark","hive") 
foreachList(b) 
val c = Array(1,2,3,4,5,6,7,8) 
println("max="+c.max+ " min="+c.min+ " sum="+c.sum) 
println(c.mkString(" ")) 
println(c.mkString("<"," ",">")) 
def foreachList(list: Array[String]): Unit ={ 
for (i <- list){ print(i+ " ") 
} 
}
}

Array.scala源码里
类名+括号.
这里就是调用了object的apply方法


05.scala集合_第1张图片
image.png

05.scala集合_第2张图片
image.png

数组转字符串用mkString,别用toString

可变数组

val d = scala.collection.mutable.ArrayBuffer[Int]() 
d+=1 
d+=2 
 d+=(3,4,5)
//可以加多个 
d++=Array(6,7,8)
//两个+加一个数组 
d.insert(0,0)
//指定位置插入 
d.remove(1)
//移除索引数据
d.trimEnd(2)
//倒叙删除 
println(d) 
for(i <- 0.until(d.length)){
 println(d(i))
 } 
println() 
for(ele <-d){ 
print(ele+" ") 
} 
println() 
for(i <- 0.until(d.length).reverse){
//反序遍历 print(i+" ") 
}
//把可变数组转变为不可变数组
d.toArray
05.scala集合_第3张图片
image.png

list

NIL为不可变的list

object ListApp extends App { 
val list = List(1,2,3,4,5) 
println(list) 
//List(1, 2, 3, 4, 5) 
println(list.head)
//1,第一个元素 
println(list.tail)
//List(2, 3, 4, 5),除第一个外都是tail 
val list1 = 1::Nil 
//List(1)
val list9=2 :: list1
//List(2,1)
val list10=1 ::2 ::3 ::Nil
//List(1,2,3)
//为定长的list println(list1) 
val list2 = scala.collection.mutable.ListBuffer[Int]()
//可变list 
list2+=1 
list2+=(2,3,4,5) 
list2++=List(6,6,7) 
list2-=2 
list2--=List(5,6) 
list2.toArray 
//转成数组 
list2.toList 
//转成定长
list list2.isEmpty 
//是否为空 
list2.head 
//头元素
} 
//递归求和 
def sum(nums:Int*):Int={ 
if(nums.length==0){
 0 
}else{ 
nums.head + sum(nums.tail:_*) 
}
 }
println(sum()) 
println(sum(1,2,3,4))
05.scala集合_第4张图片
image.png
image.png

set

可变set:scala.collection.mutable.Set

val set=scala.collection.mutable.Set[Int]()

用法与list一样 数据不重复

Map

Map(映射)是一种可迭代的键值对(key/value)结构。

所有的值都可以通过键来获取。

Map 中的键都是唯一的。

Map 也叫哈希表(Hash tables)。

Map 有两种类型,可变与不可变,区别在于可变对象可以修改它,而不可变对象不可以。

默认情况下 Scala 使用不可变 Map。如果你需要使用可变集合,你需要显式的引入 import scala.collection.mutable.Map 类

在 Scala 中 你可以同时使用可变与不可变 Map,不可变的直接使用 Map,可变的使用 mutable.Map。以下实例演示了不可变 Map 的应用:

// 空哈希表,键为字符串,值为整型

var A:Map[Char,Int] = Map()

// Map 键值对演示

val colors = Map(“red” -> “#FF0000”, “azure” -> “#F0FFFF”)

定义 Map 时,需要为键值对定义类型。如果需要添加 key-value 对,可以使用 + 号,如下所示:

A += (‘I’ -> 1)

A += (‘J’ -> 5)

A += (‘K’ -> 10)

A += (‘L’ -> 100)

object Test { 
def main(args: Array[String]) { 
val colors = Map("red" -> "#FF0000", "azure" -> "#F0FFFF", "peru" -> "#CD853F") 
val nums: Map[Int, Int] = Map() 
println( "colors 中的键为 : " + colors.keys ) println( "colors 中的值为 : " + colors.values ) 
println( "检测 colors 是否为空 : " + colors.isEmpty ) 
println( "检测 nums 是否为空 : " + nums.isEmpty ) 
}
}
05.scala集合_第5张图片
image.png

05.scala集合_第6张图片
image.png

keys 返回 Map 所有的键(key)

values 返回 Map 所有的值(value)

isEmpty 在 Map 为空时返回true

Map 合并

你可以使用 ++ 运算符或 Map.++() 方法来连接两个 Map,Map 合并时会移除重复的 key。以下演示了两个 Map 合并的实例:

object Test {

def main(args: Array[String]) {

val colors1 = Map(
“red” -> “#FF0000”,

“azure” -> “#F0FFFF”,

“peru” -> “#CD853F”)

val colors2 = Map(
“blue” -> “#0033FF”,

“yellow” -> “#FFFF00”,

“red” -> “#FF0000”)

// ++ 作为运算符 
var colors = colors1 ++ colors2 
println( "colors1 ++ colors2 : " + colors ) 
// ++ 作为方法 
colors = colors1.++(colors2) 
println( "colors1.++(colors2)) : " + colors )
}
}

执行以上代码,输出结果为:

$ scalac Test.scala

$ scala Test

colors1 ++ colors2 : Map(blue -> #0033FF, azure -> #F0FFFF, peru -> #CD853F, yellow -> #FFFF00, red -> #FF0000)

colors1.++(colors2)) : Map(blue -> #0033FF, azure -> #F0FFFF, peru -> #CD853F, yellow -> #FFFF00, red -> #FF0000)

输出 Map 的 keys 和 values

以下通过 foreach 循环输出 Map 中的 keys 和 values:


object Test {

def main(args: Array[String]) {

val sites = Map(“runoob” -> “[http://www.runoob.com](http://www.runoob.com)”,

“baidu” -> “[http://www.baidu.com](http://www.baidu.com)”,

“taobao” -> “[http://www.taobao.com](http://www.taobao.com)”)

sites.keys.foreach{ 
i => print( "Key = " + i ) 
println(" Value = " + sites(i) )
}
}
05.scala集合_第7张图片
image.png

Tuple元祖

05.scala集合_第8张图片
image.png

05.scala集合_第9张图片
image.png

你可能感兴趣的:(05.scala集合)