sacala 关于集合常用的操作

map
1.映射:对集合中的每一个元素进行执行某一项操作
2.返回值类型,正常情况不变,原来集合是什么类型,就返回什么类型
3.元素类型,根据我们函数的返回值类型

val f = (x: Int) => x * 10
val arr: Array[Int] = Array(1, 2, 3, 6, 7, 9)
//对上面数组的数字进行处理,每个数字乘以10
//第一种方法
val map1: Array[Int] = arr.map(t => t * 10)
println(map1.toList)
//第二种方法
val map2: Array[Int] = arr.map((x: Int) => x * 10)
println(map2.toList)
//第三种方法
val map3: Array[Int] = arr.map(f)
println(map3.toList)

val map4: Array[Boolean] = arr.map(x => x % 2 == 0)
// List(false, true, false, true, false, false)
println(map4.toList)

Filter
Filter方法,过滤出满足条件的数据,返回数据的类型,和之前的一模一样

val list1: List[Int] = List(1, 2, 4, 7, 8, 3)
val filter1: List[Int] = list1.filter(tp => tp % 2 == 0)
println(filter1)
val filter2: List[Int] = list1.filterNot(tp => tp % 2 == 0)
println(filter2)

//输出
List(2, 4, 8)
List(1, 7, 3)

Flatten 压平

val list: List[Array[Int]] = List(Array(1, 2, 3, 56), Array(4, 7, 8, 9))
val flatten: List[Int] = list.flatten
println(flatten)

//输出
List(1, 2, 3, 56, 4, 7, 8, 9)

FlatMap=Flatten+Map

scala> val arr=Array("hello spark","hello scala")
arr: Array[String] = Array(hello spark, hello scala)

scala> val arrSplit=arr.map(str=>str.split(" "))
arrSplit: Array[Array[String]] = Array(Array(hello, spark), Array(hello, "", scala))

scala> arrSplit.flatten
res0: Array[String] = Array(hello, spark, hello, "", scala)

scala> arr.flatMap(str=>str.split(" "))
res1: Array[String] = Array(hello, spark, hello, "", scala)

Count,find
Count:统计满足条件的个数
Find:统计满足条件的第一个元素

val arr: Array[Int] = Array(1, 2, 3, 4, 8, 75, 9, 0)
val count: Int = arr.count(t => t < 3)
println(count)
val list1: List[Int] = List(1, 2, 4, 7, 8, 3)
val count2: Int = list1.count(_ < 3)
println(count2)
val find1: Option[Int] = list1.find(_ > 3)
println(find1)

//输出
3
2
Some(4)
Foreach
对集合中每一个元素进行操作,返回值:Unit

scala> val list=List(1,4,2,6,7,9,3)
list: List[Int] = List(1, 4, 2, 6, 7, 9, 3)

scala> list.foreach(println)

GroupBy 按照指定条件来分组

scala> val arr=Array(("hello",1),("world",1),("hello",1),("spark",1))
arr: Array[(String, Int)] = Array((hello,1), (world,1), (hello,1), (spark,1))

scala> arr.groupBy(x=>x._1)
res3: scala.collection.immutable.Map[String,Array[(String, Int)]] = Map(spark -> Array((spark,1)), world -> Array((world,1)), hello -> Array((hello,1), (hello,1)))

SortBy,SortWith,Sorted
1.Sorted:默认排序
2.SortBy:参数是一个函数,指定排序的规则,默认是升序
3.SortWith:接收两个参数,两个参数进行比较
4.在函数中,如果变量只出现一次,那么此时我们可以使用_来代替

scala> val list=List(1,3,4,5,4,6,3,2,5,6)
list: List[Int] = List(1, 3, 4, 5, 4, 6, 3, 2, 5, 6)

scala> list.sorted
res5: List[Int] = List(1, 2, 3, 3, 4, 4, 5, 5, 6, 6)

scala> list.sortBy(t=>t)
res6: List[Int] = List(1, 2, 3, 3, 4, 4, 5, 5, 6, 6)

scala> list.sortWith((a,b)=>a>b)
res7: List[Int] = List(6, 6, 5, 5, 4, 4, 3, 3, 2, 1)

scala> list.sortWith((a,b)=>a<b)
res8: List[Int] = List(1, 2, 3, 3, 4, 4, 5, 5, 6, 6)

scala> list.sortWith(_>_)
res9: List[Int] = List(6, 6, 5, 5, 4, 4, 3, 3, 2, 1)

scala> list.sortWith(_<_)
res10: List[Int] = List(1, 2, 3, 3, 4, 4, 5, 5, 6, 6)

交集,并集,差集,去重
1.并集:union
2.交集:intersect
3.差集:diff
4.去重:distinct

scala> val arr=Array(1,2,4,5,6,7,2)
arr: Array[Int] = Array(1, 2, 4, 5, 6, 7, 2)

scala> val arr1=Array(1,3,4)
arr1: Array[Int] = Array(1, 3, 4)

//并集
scala> arr union arr1
res15: Array[Int] = Array(1, 2, 4, 5, 6, 7, 2, 1, 3, 4)

//交集
scala> arr intersect arr1
res16: Array[Int] = Array(1, 4)

//arr1的差集
scala> arr diff arr1
res17: Array[Int] = Array(2, 5, 6, 7, 2)

//arr的差集
scala> arr1 diff arr
res18: Array[Int] = Array(3)

//去重
scala> res15.distinct
res19: Array[Int] = Array(1, 2, 4, 5, 6, 7, 3)

Grouped(n:Int)->n是用来分组的个数
按照指定元素个数来分组

scala> val arr=Array(1,3,43,45,5,6,7,3,2,8)
arr: Array[Int] = Array(1, 3, 43, 45, 5, 6, 7, 3, 2, 8)

scala> arr.grouped(2)
res20: Iterator[Array[Int]] = non-empty iterator

scala> res20.foreach(arr=>println(arr.toList))
List(1, 3)
List(43, 45)
List(5, 6)
List(7, 3)
List(2, 8)

MkString

scala> arr
res22: Array[Int] = Array(1, 3, 43, 45, 5, 6, 7, 3, 2, 8)

scala> arr.mkString
res23: String = 134345567328

scala> arr.mkString("@")
res24: String = 1@3@43@45@5@6@7@3@2@8

scala> arr.mkString("^A")
res25: String = 1^A3^A43^A45^A5^A6^A7^A3^A2^A8

To Until其实他们都还可以指定步长

scala> 1 to 10
res31: scala.collection.immutable.Range.Inclusive = Range 1 to 10

scala> 1.to(10,2)
res32: scala.collection.immutable.Range.Inclusive = inexact Range 1 to 10 by 2

scala> 10.to(1,-3)
res33: scala.collection.immutable.Range.Inclusive = Range 10 to 1 by -3

MapValues
和map类似,只不过mapvalues处理的Map集合中的value值,key保持不变

val map1: Map[String, Int] = Map[String, Int]("a" -> 100, "b" -> 200, "c" -> 300)
//第一种方法
println("第一种")
map1.mapValues(t => t * 10).foreach(println)
//第二种方法
println("第二种")
map1.map(t => (t._1, t._2 * 10)).foreach(println)

//输出
第一种
(a,1000)
(b,2000)
(c,3000)
第二种
(a,1000)
(b,2000)
(c,3000)
Reduce ReduceLeft ReduceRight
1.都是元素的归并,元素的聚合,具体如何聚合,取决于我们的函数
2.Reduce调用的是reduceLeft,但他俩之间的区别是,reduce的参数必须是同一种类型
3.ReduceRight是从右往左进行计算,两两计算,再将结果和第三个结果进行计算
4.ReduceLeft也是,只不过是从左往右进行计算

val arr: Array[List[String]] = Array[List[String]](List("1"), List("2"))
val redu1: List[String] = arr.reduce(_ ++ _)
val redu2: List[String] = arr.reduce(_ ::: _)
println("redu1:" + redu1)
println("redu2:" + redu2)

val arr1: Array[Int] = Array(1, 2, 3, 5, 6)
val r1: Int = arr1.reduce(_ + _)
//这个地方的+代表相加
val r11: Int = arr1.reduce(_ - _)
val r12: Int = arr1.reduceLeft(_ - _)
val r13: Int = arr1.reduceRight(_ - _)
//求和
val r14: Int = arr1.reduce((a, b) => a + b)
//乘积
val r15: Int = arr1.reduce(_ * _)
println("r1:" + r1)
println("r11:" + r11)
println("r12:" + r12)
println("r13:" + r13)
println("r14:" + r14)
println("r15:" + r15)

val arr2: Array[String] = Array("a", "b", "c", "d")
val r2: String = arr2.reduce(_ + _) //这个地方的1+代表连接
println("r2:" + r2)

val arr3: Array[List[Int]] = Array(List(1, 2, 3, 5, 6), List(3, 4, 5, 7, 8))
val r3 = arr3.reduce(_ ++ _)
val r31 = arr3.reduce(_ ::: _)
val r32: List[Int] = arr3.reduceRight(_ ::: _)
println("r3:" + r3)
println("r31:" + r31)
println("r32:" + r32)
}

//输出
redu1:List(1, 2)
redu2:List(1, 2)
r1:17
r11:-15
r12:-15
r13:3
r14:17
r15:180
r2:abcd
r3:List(1, 2, 3, 5, 6, 3, 4, 5, 7, 8)
r31:List(1, 2, 3, 5, 6, 3, 4, 5, 7, 8)
r32:List(1, 2, 3, 5, 6, 3, 4, 5, 7, 8)

Fold FoldLeft FoldRight
1.Fold:带初始值的归并,和reduce相比一样,就比reduce多了有一个初始值
2.FoldLeft:从左向右归并
3.FoldRight:从右向左归并

val arr: Array[Int] = Array(1, 3, 5, 7,11)
println(arr.fold(10)(_ + _))
println(arr.foldRight(10)(_ + _))
println(arr.foldLeft(10)(_ + _))
println("=========================")
println(arr.fold(10)(_ - _))
println(arr.foldLeft(10)(_ - _))
//(1-(3-(5-(7-(11-10)))))
//两两相减
println(arr.foldRight(10)(_ - _))

//输出
37
37
37

Aggregate
Aggregate:聚合
1.单机版的aggregate调用的是fold
2.第一个函数:局部聚合
3.第二个函数:全局聚合

val arr: Array[List[Int]] = Array[List[Int]](List(1, 3), List(2), List(2, 6))
val result: Int = arr.aggregate(10)((a, b) => a + b.sum, _ + _)
println("result:" + result)

val arr1: Array[Int] = Array(1, 2, 4, 5, 6, 7, 9)
//在scala中,没有局部和全局的概念,只会执行第一个函数,第二个函数不做处理
val result2: Int = arr1.aggregate(0)(_ + _, _ * _)
println("result2:" + result2)

//输出
result:24
result2:34

初始值和累加值的补充
元素值和累加值是不固定的

val list: List[List[Int]] = List[List[Int]](List(1, 2), List(3), List(5, 7))
//初始值和累加值如何实现
//a:累加值 b:元素值
//累加值:List(1,2) 元素值(3)
//List(1,2)+List(3)=List(1,2,3)
//累加值:List(1,2,3) 元素值(5,7)
//List(1,2,3)+List(5,7)=List(1,2,3,5,7)
//reduce
val result: List[Int] = list.reduce((a, b) => (a ++ b))
//fold
val result2: List[Int] = list.fold(List(11))((a, b) => (a ++ b))
//累加值:List(11) 元素值:List(1,2)
//每个元素的最大值相加:2+5+7
val result3: Int = list.aggregate(0)((a, b) => a + b.max, _ + _)
println("redult:" + result)
println("redult2:" + result2)
println("redult3:" + result3)

//输出
redult:List(1, 2, 3, 5, 7)
redult2:List(11, 1, 2, 3, 5, 7)
redult3:12

Take(n:Int) Slice
1.take(n):获取n个元素
2.Slice:截取元素,传递的都是索引值,区间[)

val list: List[Int] = List[Int](1, 2, 3, 6, 7, 9)
//截取前两个元素
val result: List[Int] = list.take(2)
println("result:" + result)
val result2: List[Int] = list.slice(0, 3)
println("result2:" + result2)

//输出
result:List(1, 2)
result2:List(1, 2, 3)

scala集合上常用的方法的更多相关文章

  1. Ruby 集合数组常用遍历方法

    迭代器简介 先简单介绍一下迭代器. 1.一个Ruby迭代器就是一个简单的能接收代码块的方法(比如each这个方法就是一个迭代器).特征:如果一个方法里包含了yield调用,那这个方法肯定是迭代器: 2 ...

  2. Scala实战高手****第14课:Scala集合上的函数式编程实战及Spark源码鉴赏

    package com.dt.spark.scala.bascis object Functional_Itearal {   def main(args: Array[String]): Unit ...

  3. 00031_ArrayList集合中常用的方法

    1.ArrayList集合提供的一些常用方法 import java.util.ArrayList; public class ArrayListDemo01 { public static void ...

  4. 大数据学习day15----第三阶段----scala03--------1.函数(“_”的使用, 函数和方法的区别)2. 数组和集合常用的方法(迭代器,并行集合) 3. 深度理解函数 4 练习(用java实现类似Scala函数式编程的功能(不能使用Lambda表达式))

    1. 函数 函数就是一个非常灵活的运算逻辑,可以灵活的将函数传入方法中,前提是方法中接收的是类型一致的函数类型 函数式编程的好处:想要做什么就调用相应的方法(fliter.map.groupBy.so ...

  5. 大数据学习day13------第三阶段----scala01-----函数式编程。scala以及IDEA的安装,变量的定义,条件表达式,for循环(守卫模式,推导式,可变参数以及三种遍历方式),方法定义,数组以及集合(可变和非可变),数组中常用的方法

    具体见第三阶段scala-day01中的文档(scala编程基础---基础语法)  1. 函数式编程(https://www.cnblogs.com/wchukai/p/5651185.html): ...

  6. 10. Scala数据结构(上)-集合操作

    10.1 数据结构特点 10.1.1 Scala集合基本介绍 uml => 统一建模语言 1) Scala同时支持不可变集合和可变集合,不可变集合可以安全的并发访问 两个主要的包 不可变集合:s ...

  7. (转)Android之常用功能方法大集合

    这些,都是Andorid中比较常用的方法和功能,在网上搜集整理一下记录之,以备不时之需.由于经过多次转载,源文作者不确凿,在此申明,敬请见谅.不得不赞,非常实用. 1.判断sd卡是否存在 boolea ...

  8. 安装PHP过程中,make步骤报错:(集合网络上各种解决方法)

    安装PHP过程中,make步骤报错:(集合网络上各种解决方法) (1)-liconv -o sapi/fpm/php-fpm /usr/bin/ld: cannot find -liconv coll ...

  9. Javascript中常用事件集合和事件使用方法

    Javascript中常用事件集合和事件使用方法 一.事件绑定 格式: 事件源 . on事件类型=事件处理函数 事件绑定三要素 1.事件源:和谁绑定 2.事件类型:什么事件 3.事件处理函数:触发了要 ...

随机推荐

  1. 鸟哥的linux私房菜——第六章学习(Linux文件与目录管理)

    ******************第六章学习****************** 1.[文件与目录管理] 在所有目录下面都会存在的两个目录,分别是 "." 与 "..& ...

  2. 用python写的一个自动卸载python包的脚本

    import osplist=os.popen("pip list") # 执行windows cmd命令,获取所有包package列表,并获取返回结果到plist#跳过第1,2行 ...

  3. Tailwind CSS in Action

    Tailwind CSS in Action Tailwind CSS是一个高度可定制的低级CSS框架,它为您提供了构建定制设计所需的所有构造块,而无需烦恼要覆盖的烦人的自以为是的样式 https:/ ...

  4. MDN 文档高级操作进阶教程

    MDN 文档高级操作进阶教程 MDN 文档, 如何优雅的使用 MDN 文档上的富文本编辑器 pre & 语法高亮器 code & note box source code 上传附件 i ...

  5. NGINX configure auto generator

    NGINX configure auto generator The easiest way to configure a performant, secure, and stable NGINX s ...

  6. macOS 显示/隐藏 AirPlay

    macOS 显示/隐藏 AirPlay AirPlay Sidecar 必须用相同的 Apple ID 登录 mac 和 ipad, 才能使用! https://www.apple.com/macos ...

  7. node.js & Unbuntu Linux & nvm & npm

    node.js & Unbuntu Linux & nvm & npm https://websiteforstudents.com/install-the-latest-no ...

  8. taro swiper & scroll tabs

    taro swiper & scroll tabs https://taro-docs.jd.com/taro/docs/components/viewContainer/swiper.htm ...

  9. “Fatal error: Unable to find local grunt.” when running “grunt” command

    下载到本地 >npm install grunt >grunt 命令行运行:grunt,出现以下问题: 这些是Gruntfile.js中引用的,依次安装: npm install grun ...

  10. NGK公链:夯实基础设施 实现产业大规模应用

    当前,区块链已经成为全球技术角逐的前沿,大国及科技巨头竞相在该领域布局,引导区块链服务实体经济,激发市场经济活力.据市场相关研究机构预测,2020年,基于区块链的业务将达到1000亿美元. 对于区块链 ...