• scala 基础


    1.scala一些预热操作

    1.1 to 是一个方法,()可以进行 参数传递,map()把每一个元素取出来进行相应的操作,

    1.  print(1.to(10).map(_*10))
    2.  结果
    3.  Vector(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)

    1.2取数组中的每一个值

    1.  val arr=Array(1,2,3,4,5,6,7,8,9)
    2.  //直接遍历每一个值
    3.  for (i<-arr) print(i+" ")
    4.  //通过下标遍历每一个值 until是取值左闭右开
    5.  for(i <- 0 until arr.length) println(arr(i))

    1.3 List的flatten 可以将一个list嵌套list、list嵌套字符串压平

    1.  val ls1=List(1,2,3,4,5,6,7,8,9)
    2.  val ls2=ls1.grouped(5)
    3.  val ls3=ls2.toList
    4.  println(ls3) //List(List(1, 2, 3, 4, 5), List(6, 7, 8, 9))
    5.  println(ls3.flatten) //list套list有压平操作 也可以压平 list中套多个字符串的情况
    6.  结果:list(1, 2, 3, 4, 5, 6, 7, 8, 9)

    1.4取一个元组的第n个值

    1.  val t=(1,2,3,4,4,5,6)
    2.  print(t._2) ////去元组的第n个

    2.wordcount

    1.  val lines=List("hello tom hello jerry","hello tom hello kitty hello china")
    2.  方法一:
    3.  val wc=lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).map(t=>(t._1,t._2.size)).toList.sortBy(_._2).reverse
    4.  方法二:
    5.  val wc2=lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues(_.size)
    6.  方法三:
    7.  val wc3=lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2))
    8.  如果是在spark上:
    9.  val wc4=lines.flatMap(_.split(" ")).map((_,1)).reduceByKey(_+_).sortBy(_._2,false).collect

    2.1flatMap=map+flatten

    1.  val words=lines.flatMap(_.split(" "))
    2.  结果:
    3.  List(hello, tom, hello, jerry, hello, tom, hello, kitty, hello, china)

    可以拆分map过程

    1.  lines.map(_.split(" "))
    2.  拿到每一个元素(字符串),按照空格切割,切割后返回两个数组,仍放在List中
    3.  res0: List[Array[String]] = List(Array(hello, tom, hello, jerry), Array(hello, tom, hello, kitty, hello, china))

    flatten过程

    1.  lines.map(_.split(" ")).flatten
    2.  List(hello, tom, hello, jerry, hello, tom, hello, kitty, hello, china)

    2.2 将单词出现一次和1放在一起(放入元组)

    1.  val wordsAndOne=words.map((_,1))
    2.  List((hello,1), (tom,1), (hello,1), (jerry,1), (hello,1), (tom,1), (hello,1), (kitty,1), (hello,1), (china,1))

    2.3 groupBy按照单词分组返回map

    1.  第一个_ 表示List中的每一个元组,取元组中的某一个元素用._n,即按照元组中的某一元素分组,返回是一个map
    2.  val grouped =wordsAndOne.groupBy(_._1)
    3.  Map(kitty -> List((kitty,1)), china -> List((china,1)), tom -> List((tom,1), (tom,1)), hello -> List((hello,1), (hello,1), (hello,1), (hello,1), (hello,1)), jerry -> List((jerry,1)))

    2.4求和

    1.  t就是每一个对偶元组, 仍返回一个map
    2.  val result = grouped.map(t=>(t._1,t._2.size))
    3.  Map(kitty -> 1, china -> 1, tom -> 2, hello -> 5, jerry -> 1)

    直接对第一个单词和list.size 但scala中不予许这样的操作

    1.  第一个_ 拿到一个对偶元组,取元组中的第一个元组。_._2.size即是取对偶元组第二个的大小。
    2.  val result=grouped.map(_._1,_._2.size)

    2.5 按出现次数从大到小排序M:默认自然排序, map没有sortBy 先将map.toList

    1.  val finalResult=result.toList.sortBy(_._2).reverse
    2.  List((hello,5), (tom,2), (jerry,1), (china,1), (kitty,1)

    3.方法二中:mapValues的_ 指的是元组的值 即v。key 不动,只是对values进行处理  结果 k v 一起返回

    val wc2=lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues(_.size)
     

    4.方法三中:

    fold(0)(_+_)  0 是初始值
    Map(kitty -> List((kitty,1)), china -> List((china,1)), tom -> List((tom,1), (tom,1)), hello -> List((hello,1), (hello,1), (hello,1), (hello,1), (hello,1)), jerry -> List((jerry,1)))
    foldLeft(0)(_+_._2)第一个_表示初始值或者上一次累加的结果 中第二个_ ,表示拿到的每一个元组,第三个元组的中第n个值

    val wc3=lines.flatMap(_.split(" ")).map((_,1)).groupBy(_._1).mapValues(_.foldLeft(0)(_+_._2))
     

    5.reduce和fold操作

    1.  val a=Array(1,2,3,4,5,6)
    2.  println(a.sum) //求和 21
    3.  println(a.reduce(_+_))//21 聚合操作要求传递两个参数
    4.  println(a.reduce(_-_))//-19 也可以进行减法操作

    a.reduce(_+_) 默认调用 a.reduceLift   (((((1+2)+3)+4)+5)+6)

    
    
    println(a.par.reduce(_+_)) //21  par转化成并行化操作
     

    fold 也支持并行化--并行计算,及柯里化

    1.  val b=Array(1,2,3,4,5,6)
    2.  println(b.fold(10)(_+_)) //31和cpu核心数无关
    3.  println((b.par.fold(10)(_+_))) //61和cpu核心数有关,2核心4线程,所以4*10
    4.  println((b.par.fold(0)(_+_))) //21
    5.   
    6.  println(b.foldLeft(10)(_+_)) //31
    7.  println(b.foldRight(10)(_+_)) //31
  • 相关阅读:
    本博客主题的相关配置(2021年)
    vscode侧边栏隐藏不需要的文件
    虎扑,豆瓣等用css屏蔽广告代码
    代替pandownload的百度网盘下载软件
    网络请求的超时原因
    OkHttp3系列(三)okhttp3-fast-spring-boot-starter
    OkHttp3系列(二)MockWebServer使用
    OkHttp3系列(一)初识OkHttp3
    为什么要使用短链接
    Google Guava之简化异常和错误的传播与检查
  • 原文地址:https://www.cnblogs.com/beiyi888/p/9700228.html
Copyright © 2020-2023  润新知