import scala.collection.mutable.ArrayBuffer
/**
学习Scala的集合类型,常用的包含:
Array,List,Set,Map,Tuple
本节是学习Array的声明,使用和重要的方法
**/
object Demo04 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
//--声明一个定长数组,并赋值
val a1=Array(1,2,3,4) //> a1 : Array[Int] = Array(1, 2, 3, 4)
//--声明一个定长的空数组,并指定泛型类型
//--scala的泛型用的[] 不同于java的<>
val a2=new Array[Int](3) //> a2 : Array[Int] = Array(0, 0, 0)
//--声明变长数组
val a3=ArrayBuffer(1,2,3,4) //> a3 : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4)
//--声明变长数组
val a4=scala.collection.mutable.ArrayBuffer(1,2,3,4)
//> a4 : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4)
//--mutable:可变的 immutable:不可变的
//--apply是下标操作的方法,scala的下标操作符号是(),不同于java的[]
a1.apply(0) //> res0: Int = 1
//--apply可以省略
a1(0) //> res1: Int = 1
//--通过下标赋值
a1(0)=10
a1 //> res2: Array[Int] = Array(10, 2, 3, 4)
//--针对变长数组,因为长度可变,可以采用追加元素操作
for(i<-1 to 5){
a3.append(i)
}
a3 //> res3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4, 1,
//| 2, 3, 4, 5)
//--接下来,学习关于Array重要的函数方法
val a5=Array(3,1,4,6,5,2,1,1,2,3) //> a5 : Array[Int] = Array(3, 1, 4, 6, 5, 2, 1, 1, 2, 3)
//--最大值
a5.max //> res4: Int = 6
//--最小值
a5.min //> res5: Int = 1
//--去重之后,返回新Array
val r1=a5.distinct //> r1 : Array[Int] = Array(3, 1, 4, 6, 5, 2)
//--取出前n个元素,并返回新Array
val r2=a5.take(2) //> r2 : Array[Int] = Array(3, 1)
//--取出尾n个元素,并返回新Array
val r3=a5.takeRight(2) //> r3 : Array[Int] = Array(2, 3)
//--删除前n个元素,返回剩余元素并返回新Array
val r4=a5.drop(2) //> r4 : Array[Int] = Array(4, 6, 5, 2, 1, 1, 2, 3)
//--删除尾n个元素,返回剩余元素并返回新Array
val r5=a5.dropRight(2) //> r5 : Array[Int] = Array(3, 1, 4, 6, 5, 2, 1, 1)
//--有别与take(1),返回的是头元素
val r6=a5.head //> r6 : Int = 3
val r7=a5.take(1) //> r7 : Array[Int] = Array(3)
//--取出尾元素,区别takeRight(1)
val r8=a5.last //> r8 : Int = 3
val r9=a5.takeRight(1) //> r9 : Array[Int] = Array(3)
//--将数组元素返回成字符串,并指定元素间的分隔符
val r10=a5.mkString("|") //> r10 : String = 3|1|4|6|5|2|1|1|2|3
//--遍历
a5.foreach{println(_)} //> 3
//| 1
//| 4
//| 6
//| 5
//| 2
//| 1
//| 1
//| 2
//| 3
//--根据匿名函数来实现过滤
a5.filter { x => x>4&&x%2==0 } //> res6: Array[Int] = Array(6)
//--根据匿名函数,判断元素是否存在
a5.exists{x=>x>4&&x%2==0} //> res7: Boolean = true
val a6=Array(1,2,3) //> a6 : Array[Int] = Array(1, 2, 3)
val a7=Array(3,4,5) //> a7 : Array[Int] = Array(3, 4, 5)
val a8=Array(3,6,8) //> a8 : Array[Int] = Array(3, 6, 8)
//--取交集。比如在比较文件中相同数据时,
val r11=a6.intersect(a7).intersect(a8) //> r11 : Array[Int] = Array(3)
//--取差集,比如在比较文件的相异数据时
val r12=a6.diff(a7) //> r12 : Array[Int] = Array(1, 2)
val r13=a7.diff(a6) //> r13 : Array[Int] = Array(4, 5)
//--拼接多个数组,并返回新数组
val r14=Array.concat(a6,a7,a8) //> r14 : Array[Int] = Array(1, 2, 3, 3, 4, 5, 3, 6, 8)
//--拼接定长和变长
val r15=ArrayBuffer.concat(a6,a7,a8,a3) //> r15 : scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 3,
//| 4, 5, 3, 6, 8, 1, 2, 3, 4, 1, 2, 3, 4, 5)
//--创建一个区间范围的数组,并指定步长
val r16=Array.range(0,5,2) //> r16 : Array[Int] = Array(0, 2, 4)
//--创建一个将匿名函数应用于初始值的数组
val r17=Array.iterate(0,6)(x=>x*2) //> r17 : Array[Int] = Array(0, 0, 0, 0, 0, 0)
//--将匿名函数应用数组下标
val r18=Array.tabulate(4)(x=>x*2) //> r18 : Array[Int] = Array(0, 2, 4, 6)
//--求和
val r19=a6.sum //> r19 : Int = 6
val a9=Array(1,6,2,3,4,5) //> a9 : Array[Int] = Array(1, 6, 2, 3, 4, 5)
//--对数组进行排序,升序排序
scala.util.Sorting.quickSort(a9)
a9 //> res8: Array[Int] = Array(1, 2, 3, 4, 5, 6)
//--反转
a9 reverse //> res9: Array[Int] = Array(6, 5, 4, 3, 2, 1)
//--sortBy是排序之后返回一个新Array
val r20=a9.sortBy{x=>x} //> r20 : Array[Int] = Array(1, 2, 3, 4, 5, 6)
//-- 降序排序,用-前缀操作符,注意有空格
val r21=a9.sortBy{x=> -x} //> r21 : Array[Int] = Array(6, 5, 4, 3, 2, 1)
}
import scala.collection.mutable.ListBuffer
/**
学习List
**/
object Demo05 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
//--声明定长List
val l1=List(1,2,3,4) //> l1 : List[Int] = List(1, 2, 3, 4)
//--声明变长List
val l2=ListBuffer(1,2,3,4) //> l2 : scala.collection.mutable.ListBuffer[Int] = ListBuffer(1, 2, 3, 4)
//--通过下标取值
l1.apply(0) //> res0: Int = 1
l1(0) //> res1: Int = 1
//--声明空List
val l3=List[Nothing]() //> l3 : List[Nothing] = List()
//--判断List是否为空
l3.isEmpty //> res2: Boolean = true
val l4=List(1,2,3) //> l4 : List[Int] = List(1, 2, 3)
val l5=List(3,4,5) //> l5 : List[Int] = List(3, 4, 5)
//--拼接List
val r1=l4:::l5:::l1 //> r1 : List[Int] = List(1, 2, 3, 3, 4, 5, 1, 2, 3, 4)
val r2=List.concat(l4,l5,l1) //> r2 : List[Int] = List(1, 2, 3, 3, 4, 5, 1, 2, 3, 4)
//--用指定元素,填充List
val r3=List.fill(5)("a") //> r3 : List[String] = List(a, a, a, a, a)
//--在左侧追加元素,返回List
val r4=0+:l4 //> r4 : List[Int] = List(0, 1, 2, 3)
//--在右侧追加元素,返回List
val r5=l5:+4 //> r5 : List[Int] = List(3, 4, 5, 4)
l4.contains(3) //> res3: Boolean = true
val a1=new Array[Int](3) //> a1 : Array[Int] = Array(0, 0, 0)
//--将List元素拷贝到Array
l4.copyToArray(a1, 0, 3)
a1 //> res4: Array[Int] = Array(1, 2, 3)
//--List转换成Array
val a2=l4.toArray //> a2 : Array[Int] = Array(1, 2, 3)
val l6=List(1,2,3,1,2,3) //> l6 : List[Int] = List(1, 2, 3, 1, 2, 3)
l6.indexOf(1, 1) //> res5: Int = 3
//--重点记map方法,映射方法,作用是将集合中的元素从一个形式映射到另一个形式
//--而且不会概念集合的元素个数
val l7=List(1,2,3,4) //> l7 : List[Int] = List(1, 2, 3, 4)
val l8=l7.map{ x => x*2 } //> l8 : List[Int] = List(2, 4, 6, 8)
val l9=l7.map { x => x.toString() } //> l9 : List[String] = List(1, 2, 3, 4)
val l10=List("hello,world","hello,hadoop","hello,scala")
//> l10 : List[String] = List(hello,world, hello,hadoop, hello,scala)
val l11=l10.map { x => x.split(",") } //> l11 : List[Array[String]] = List(Array(hello, world), Array(hello, hadoop)
//| , Array(hello, scala))
//--reduceLeft是迭代方法
//--第一次迭代:a=1 b=2 a+b=3
//--第二次迭代:a=3 b=3 a+b=6
//--第三次迭代:a=6 b=4 a+b=10
l7.reduceLeft{(a,b)=>a+b} //> res6: Int = 10
//--要求用reduceLeft计算1~10的阶乘结果
val l12=Array.range(1,11).toList //> l12 : List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
l12.reduceLeft{(a,b)=>a*b} //> res7: Int = 3628800
l12.reduceLeft{(a:Int,b:Int)=>{a*b}} //> res8: Int = 3628800
l12.reduceLeft{_*_} //> res9: Int = 3628800
}
/**
Array和List通用而且重要的方法
1.take
2.takeRight
3.drop
4.dropRight
5.head
6.last
7.mkString
8.filter
9.exists
10.length
11.foreach
12.intersect
13.diff
14.map(映射)
15.sortBy(排序)
16.flatMap
17.reduce(reduceLeft)
18.groupBy
19.mapValues(只有Map类型有,操作map的value的)
再重点掌握:map,filter,sortBy,flatMap,reduce,groupBy,mapValues
**/
object Demo06 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
val l1=List("hello,world","hello,hadoop","hello,scala")
//> l1 : List[String] = List(hello,world, hello,hadoop, hello,scala)
//--映射方法,不会改变元素个数
l1.map { x => x.split(",") } //> res0: List[Array[String]] = List(Array(hello, world), Array(hello, hadoop),
//| Array(hello, scala))
//--扁平化map,也是映射方法。会改变元素个数
l1.flatMap { x => x.split(",") } //> res1: List[String] = List(hello, world, hello, hadoop, hello, scala)
val l2=List("bj","sh","bj","bj","sh") //> l2 : List[String] = List(bj, sh, bj, bj, sh)
//--按匿名函数条件做分组
l2.groupBy { x => x } //> res2: scala.collection.immutable.Map[String,List[String]] = Map(bj -> List(b
//| j, bj, bj), sh -> List(sh, sh))
val l3=List((1,"bj"),(3,"sh") ,(2,"bj"),(4,"bj"))
//> l3 : List[(Int, String)] = List((1,bj), (3,sh), (2,bj), (4,bj))
//--操作l3,按地区分组
l3.groupBy{x=>x._2} //> res3: scala.collection.immutable.Map[String,List[(Int, String)]] = Map(bj ->
//| List((1,bj), (2,bj), (4,bj)), sh -> List((3,sh)))
val l4=List("hello","world","hello","hello","world")
//> l4 : List[String] = List(hello, world, hello, hello, world)
//--统计l4中单词出现的频次。("hello",3) ("world",2)
//--提示:map,groupBy
l4.groupBy { word =>word}.map{case(k,v)=>(k,v.size)}
//> res4: scala.collection.immutable.Map[String,Int] = Map(world -> 2, hello ->
//| 3)
//--统计l4单词频次,用mapValues,groupBy来实现
l4.groupBy { word =>word}.mapValues { x => x.size }
//> res5: scala.collection.immutable.Map[String,Int] = Map(world -> 2, hello ->
//| 3)
//--接着统计出单词频次,要求,不能用list.size或 length
//--提示:sum或reduce来求出频次
l4.map { word =>(word,1)}
.groupBy{case(word,one)=>word}
.map { case(k,v) =>(k,v.map{x=>x._2}.sum) } //> res6: scala.collection.immutable.Map[String,Int] = Map(world -> 2, hello ->
//| 3)
val l5=List("hello world","hello hadoop","hello hadoop")
//> l5 : List[String] = List(hello world, hello hadoop, hello hadoop)
//--统计出l5的单词频次
//--提示和要求:flatMap,groupBy,Mapvalues,用reduce来求出频次
l5.flatMap { line => line.split(" ") }
.map { word =>(word,1) }
.groupBy{case(word,one)=>word}
.mapValues{list=>list.map{x=>x._2}.reduce(_+_)}//reduce表示规约操作
//> res7: scala.collection.immutable.Map[String,Int] = Map(hadoop -> 2, world -
//| > 1, hello -> 3)
l5.flatMap { _.split(" ") }
.map { (_,1) }
.groupBy{_._1}
.mapValues{_.map{_._2}.reduce(_+_)} //> res8: scala.collection.immutable.Map[String,Int] = Map(hadoop -> 2, world -
//| > 1, hello -> 3)
}
/**
学习Set
**/
object Demo07 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
//---声明一个定长Set,Set是不包含重复元素
val s1=Set(1,1,2,3,4) //> s1 : scala.collection.immutable.Set[Int] = Set(1, 2, 3, 4)
//--声明一个变长Set
val s2=scala.collection.mutable.Set(1,2,3,4) //> s2 : scala.collection.mutable.Set[Int] = Set(1, 2, 3, 4)
val s3=Set(3,4,5) //> s3 : scala.collection.immutable.Set[Int] = Set(3, 4, 5)
//--取交集
val s4=s1&s3 //> s4 : scala.collection.immutable.Set[Int] = Set(3, 4)
val s5=s1.intersect(s3) //> s5 : scala.collection.immutable.Set[Int] = Set(3, 4)
//--取差集
val s6=s1&~s3 //> s6 : scala.collection.immutable.Set[Int] = Set(1, 2)
val s7=s1.diff(s3) //> s7 : scala.collection.immutable.Set[Int] = Set(1, 2)
//--合并
val s8=s1++s3 //> s8 : scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)
//--根据匿名函数做统计
s1.count { x => x>2 } //> res0: Int = 2
val s9=Set(1,2,3,4,5,6,7) //> s9 : scala.collection.immutable.Set[Int] = Set(5, 1, 6, 2, 7, 3, 4)
//--将一个Set拆成两个子Set
s9.splitAt(4) //> res1: (scala.collection.immutable.Set[Int], scala.collection.immutable.Set[I
//| nt]) = (Set(5, 1, 6, 2),Set(7, 3, 4))
}
/**
学习Map
**/
object Demo08 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
//--声明定长map
val m1=Map("tom"->23,"rose"->25,"jary"->30) //> m1 : scala.collection.immutable.Map[String,Int] = Map(tom -> 23, rose -> 25
//| , jary -> 30)
//--声明变长map
val m2=scala.collection.mutable.Map("tom"->23,"rose"->25)
//> m2 : scala.collection.mutable.Map[String,Int] = Map(rose -> 25, tom -> 23)
//|
//--通过key获取value
m1.apply("tom") //> res0: Int = 23
m1("tom") //> res1: Int = 23
//--get方法也是通过key取值,区别在于返回的值封装到Some类型中
//--此方法,可以指定当查询的key不存在时,设置默认值
m1.get("wefowi").getOrElse(0) //> res2: Int = 0
//--获取map的key的迭代器
val r1=m1.keys //> r1 : Iterable[String] = Set(tom, rose, jary)
//--获取map的value迭代器
val r2=m1.values //> r2 : Iterable[Int] = MapLike(23, 25, 30)
r1.foreach{println} //> tom
//| rose
//| jary
//--针对变长map,追加kv对
m2+=("jim"->35) //> res3: Demo08.m2.type = Map(jim -> 35, rose -> 25, tom -> 23)
m1.contains("tom") //> res4: Boolean = true
//--根据匿名函数实现过滤
m1.filter{case(k,v)=>v>25} //> res5: scala.collection.immutable.Map[String,Int] = Map(jary -> 30)
//--专门操作map类型的value,实现映射
m1.mapValues { x =>x+1 } //> res6: scala.collection.immutable.Map[String,Int] = Map(tom -> 24, rose -> 26
//| , jary -> 31)
m1.map{case(k,v)=>(k,v+1)} //> res7: scala.collection.immutable.Map[String,Int] = Map(tom -> 24, rose -> 26
//| , jary -> 31)
}
/**
学习Tuple(元组),是最重要和最常用的数据类型
**/
object Demo09 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
//--声明了一个元素,包含了4个元素
val t1=(1,2,3,4) //> t1 : (Int, Int, Int, Int) = (1,2,3,4)
val t2=(10,"hello",Array(1,2,3),List(2,3,4)) //> t2 : (Int, String, Array[Int], List[Int]) = (10,hello,Array(1, 2, 3),List(2
//| , 3, 4))
//--元组的取值方式
t2._2 //> res0: String = hello
val t3=(1,2,3,(1,2,3,4),("hello","world",Array(1,2,3)))
//> t3 : (Int, Int, Int, (Int, Int, Int, Int), (String, String, Array[Int])) =
//| (1,2,3,(1,2,3,4),(hello,world,Array(1, 2, 3)))
t3._4._4 //> res1: Int = 4
t3._5._2 //> res2: String = world
t3._5._3(0) //> res3: Int = 1
val m1=Map("tom"->23,"rose"->35,"jary"->30) //> m1 : scala.collection.immutable.Map[String,Int] = Map(tom -> 23, rose -> 35
//| , jary -> 30)
//--操作m1,过滤出年龄>30的数据
m1.filter{case(k,v)=>v>30} //> res4: scala.collection.immutable.Map[String,Int] = Map(rose -> 35)
m1.filter{x=>x._2>30} //> res5: scala.collection.immutable.Map[String,Int] = Map(rose -> 35)
val l1=List("hello","world","hello","hadoop") //> l1 : List[String] = List(hello, world, hello, hadoop)
//--操作l1,用map做映射 List[String]->List[(String,1)]
val l2=l1.map { word =>(word,1) } //> l2 : List[(String, Int)] = List((hello,1), (world,1), (hello,1), (hadoop,1)
//| )
//--操作l2,用map映射 List[(String,1)]->List[String]
val l3=l2.map{x=>x._1} //> l3 : List[String] = List(hello, world, hello, hadoop)
val l4=List(("bj",2),("sh",4),("gz",1),("sz",3))//> l4 : List[(String, Int)] = List((bj,2), (sh,4), (gz,1), (sz,3))
val l5=l4.filter{x=>x._2>2} //> l5 : List[(String, Int)] = List((sh,4), (sz,3))
//--根据指定的条件做升序排序
//--降序排序用 前缀操作符 -
val l6=l4.sortBy{x=> -x._2} //> l6 : List[(String, Int)] = List((sh,4), (sz,3), (bj,2), (gz,1))
}
Scala集合类
最新推荐文章于 2022-03-02 11:13:32 发布