Scala学习(集合02)
Posted 数据攻城小狮子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Scala学习(集合02)相关的知识,希望对你有一定的参考价值。
文章目录
元组
package learn04
object demo15
def main(args: Array[String]): Unit =
//创建元组,使用小括号
val tuple=(1,"xwk",30)
//根据顺序号访问元组的数据
println(tuple._1)
println(tuple._2)
println(tuple._3)
//迭代器
val iterator:Iterator[Any]=tuple.productIterator
//根据索引访问元素
println(tuple.productElement(0))
//如果元组的元素只有两个,那么我们称之为对偶元组,也称之为键值对
val kv:(String,Int)=("a",1)
val kv1:(String,Int)="a"->1
println(kv eq kv1)
1
xwk
30
1
false
队列
package learn04
import scala.collection.mutable
object demo16
def main(args: Array[String]): Unit =
val que=new mutable.Queue[String]()
//添加元素
que.enqueue("a","b","c")
val que1:mutable.Queue[String]=que+="d"
println("que:"+que.mkString(","))
println("que1:"+que1.mkString(","))
println(que eq que1)
//获取元素
println(que.dequeue())
println(que.dequeue())
println(que.dequeue())
que:a,b,c,d
que1:a,b,c,d
true
a
b
c
并行
package learn04
object demo17
def main(args: Array[String]): Unit =
/*
Scala为了充分使用多核CPU,
提供了并行集合(有别于前面的串行集合),
用于多核环境的并行计算。
*/
val result1=(0 to 100).mapx=>Thread.currentThread.getName
val result2=(0 to 100).par.mapx=>Thread.currentThread.getName
println(result1)
println(result2)
常用方法
常用方法
package learn04
object demo18
def main(args: Array[String]): Unit =
val list=List(1,2,3,4)
//集合长度
println("size=>"+list.size)
println("length=>"+list.length)
//判断集合是否为空
println("isEmpty=>"+list.isEmpty)
//集合迭代器
println("iterator=>"+list.iterator)
//循环遍历集合
list.foreach(print)
//将集合转换为字符串
println("mkString=>"+list.mkString(","))
//判断集合中是否包含某个元素
println("contains=>"+list.contains(2))
//取集合的前几个元素
println("take=>"+list.take(2))
//取集合的后几个元素
println("takeRight=>"+list.takeRight(2))
//查找元素
println("find=>"+list.find(x=>x%2==0))
//丢弃前几个元素
println("drop=>"+list.drop(2))
//丢弃后几个元素
println("dropRight=>"+list.dropRight(2))
//反转集合
println("reverse=>"+list.reverse)
//去重
println("distinct=>"+list.distinct)
size=>4
length=>4
isEmpty=>false
iterator=><iterator>
1234mkString=>1,2,3,4
contains=>true
take=>List(1, 2)
takeRight=>List(3, 4)
find=>Some(2)
drop=>List(3, 4)
dropRight=>List(1, 2)
reverse=>List(4, 3, 2, 1)
distinct=>List(1, 2, 3, 4)
衍生集合
package learn04
object demo19
def main(args: Array[String]): Unit =
val list=List(1,2,3,4)
val list1=List(1,2,3,4)
val list2=List(3,4,5,6)
//集合头
println("head=>"+list.head)
//集合尾
println("tail=>"+list.tail)
//集合尾迭代
println("tails=>"+list.tails)
//集合初始值
println("init=>"+list.init)
//集合初始值迭代
println("inits=>"+list.inits)
//集合最后元素
println("last=>"+list.last)
//集合并集
println("union=>"+list.union(list1))
//集合交集
println("intersect=>"+list.intersect(list1))
//集合差集
println("diff=>"+list.diff(list1))
//切分集合
println("splitAt=>"+list.splitAt(2))
//滑动(窗口)
println("sliding=>"+list.sliding(2))
//滚动(没有重复)
println("sliding=>"+list.sliding(2,2))
//拉链
println("zip=>"+list.zip(list1))
//数据索引拉链
println("zipWithIndex=>"+list.zipWithIndex)
head=>1
tail=>List(2, 3, 4)
tails=><iterator>
init=>List(1, 2, 3)
inits=><iterator>
last=>4
union=>List(1, 2, 3, 4, 1, 2, 3, 4)
intersect=>List(1, 2, 3, 4)
diff=>List()
splitAt=>(List(1, 2),List(3, 4))
sliding=><iterator>
sliding=><iterator>
zip=>List((1,1), (2,2), (3,3), (4,4))
zipWithIndex=>List((1,0), (2,1), (3,2), (4,3))
计算函数
package learn04
object demo20
def main(args: Array[String]): Unit =
val list=List(1,2,3,4)
val list1=List(3,4,5,6)
//集合最小值
println("min=>"+list.min)
//集合最大值
println("max=>"+list.max)
//集合求和
println("sum=>"+list.sum)
//集合乘积
println("product=>"+list.product)
//集合简化规约
println("reduce=>"+list.reduce((x:Int,y:Int)=>x+y))
println("reduce=>"+list.reduce((x,y)=>x+y))
println("reduce=>"+list.reduce((x,y)=>x+y))
println("reduce=>"+list.reduce(_+_))
//集合简化规约(左)
println("reduceLeft=>"+list.reduceLeft(_+_))
//集合简化规约(右)
println("reduceRight=>"+list.reduceRight(_+_))
//集合折叠
println("fold=>"+list.fold(0)(_+_))
//集合折叠(左)
println("foldLeft=>"+list.foldLeft(0)(_+_))
//集合折叠(右)
println("foldRight=>"+list.foldRight(0)(_+_))
//集合扫描
println("scan=>"+list.scan(0)(_+_))
//集合扫描(左)
println("scanLeft=>"+list.scanLeft(0)(_+_))
//集合扫描(右)
println("scanRight=>"+list.scanRight(0)(_+_))
min=>1
max=>4
sum=>10
product=>24
reduce=>10
reduce=>10
reduce=>10
reduce=>10
reduceLeft=>10
reduceRight=>10
fold=>10
foldLeft=>10
foldRight=>10
scan=>List(0, 1, 3, 6, 10)
scanLeft=>List(0, 1, 3, 6, 10)
scanRight=>List(10, 9, 7, 4, 0)
功能函数
package learn04
object demo21
def main(args: Array[String]): Unit =
val list=List(1,2,3,4)
//集合映射
println("map=>"+list.map(x=>x*2))
println("map=>"+list.map(x=>x*2))
println("map=>"+list.map(_*2))
//集合扁平化
val list1=List(
List(1,2),
List(3,4)
)
println("flatten=>"+list1.flatten)
//集合扁平映射
println("flatMap=>"+list1.flatMap(list=>list))
//集合过滤数据
println("filter=>"+list.filter(_%2==0))
//集合分组数据
println("groupBy=>"+list.groupBy(_%2))
//集合排序
println("sortBy=>"+list.sortBy(num=>num)(Ordering.Int.reverse))
println("sortWith=>"+list.sortWith((left,right)=>left<right))
map=>List(2, 4, 6, 8)
map=>List(2, 4, 6, 8)
map=>List(2, 4, 6, 8)
flatten=>List(1, 2, 3, 4)
flatMap=>List(1, 2, 3, 4)
filter=>List(2, 4)
groupBy=>Map(1 -> List(1, 3), 0 -> List(2, 4))
sortBy=>List(4, 3, 2, 1)
sortWith=>List(1, 2, 3, 4)
案例实现 - WordCount TopN
words.txt
Hello Scala
Hello Spark
Hello Hadoop
package learn04
import scala.io.Source
object demo22
def main(args: Array[String]): Unit =
val list:List[String]=Source.fromFile("./src/learn04/words.txt").getLines().toList
val wordList:List[String]=list.flatMap(_.split(" "))
val word2OneList:List[(String,Int)]=wordList.map((_,1))
val word2ListMap:Map[String,List[(String,Int)]]=word2OneList.groupBy(_._1)
val word2CountMap: Map[String, Int] = word2ListMap.map(
kv =>
(kv._1, kv._2.size)
)
println(word2CountMap)
Map(Hadoop -> 1, Hello -> 3, Spark -> 1, Scala -> 1)
以上是关于Scala学习(集合02)的主要内容,如果未能解决你的问题,请参考以下文章