美文网首页
05.scala集合

05.scala集合

作者: shone_shawn | 来源:发表于2019-08-22 15:50 被阅读0次

数组

不可变数组:

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方法


image.png
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
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))
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 ) 
}
}
image.png
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) )
}
}
image.png

Tuple元祖

image.png
image.png

相关文章

  • 05.scala集合

    数组 不可变数组: Array.scala源码里类名+括号.这里就是调用了object的apply方法 数组转字符...

  • 我的Swift的学习总结 -->第二周

    集合 集合:Set,定义一个集合可以写成:var 集合名 : Set<集合类型> = [集合元素],具体的集合应用...

  • markdown 测试

    集合 集合 集合 引用

  • kotlin学习第五天:集合,高阶函数,Lambda表达式

    集合 list集合 list集合分为可变集合与不可变集合。由list of创建的集合为不可变集合,不能扩容,不能修...

  • kotlin练习 ---- 集合练习

    kotlin练习 - 集合练习 Set集合 Set集合创建 Set集合的使用 List集合 List集合创建 Li...

  • 集合总结

    集合 集合分为单列集合和双列集合两种: 一.单列集合: Collection是单列集合的顶级接口: 其中有三类集合...

  • 映射、元组、集合

    映射 元组 集合 集合之seq 集合之set 集合之map

  • 16.Collection集合

    主要内容: Collection 集合 迭代器 增强for List 集合 Set 集合 1,集合 集合是java...

  • 集合与有序集合

    集合分为有序集合 (zset) 和无序集合 (set), 一般无序集合也直接说成集合 无序集合 (set) 无序集...

  • python入坑第八天|集合

    好的,各位蛇友,我们今天来学习集合。 内容: 集合的创建 集合操作符号 集合的内置函数 集合的创建 集合用set(...

网友评论

      本文标题:05.scala集合

      本文链接:https://www.haomeiwen.com/subject/xagwsctx.html