Scala集合笔记
Scala的集合框架类比Java提供了更多的一些方便的api,使得使用scala编程时代码变得非常精简,尤其是在Spark中,很多功能都是由scala的这些api构成的,所以,了解这些方法的使用,将更加有助于我们学习Scala和Spark:
List,Map,Set的一些api的用法如下:
- /**
- * Scala 集合测试
- */
- def collectionTest(): Unit ={
- val map =Map("red"->"红色","green"->"绿色")
- val colors = List("red", "green", "blue")
- //map函数测试 ,打印长度
- val size=colors.map( (f:String) => f.size );
- val data=List(1,2,5,3);
- //reduce函数测试 求和
- val r=data.reduce((a:Int,b:Int)=>a+b);
- println(size," ",r)
- //打印取值
- println(map("red"))
- //判断是否存在
- println(map.contains("white"))
- //遍历map集合
- for(pair<-map) println(pair)
- //去重打印
- println(List(3,2,3,4,5).distinct)
- //fitler过滤
- println(List(3,12,33,64,15).filter(_ > 18))
- //扁平化处理
- println(List(List(1, 2), List(3, 4)).flatten)
- //partition 分区
- println(List(1, 2, 3, 4, 5) partition (_ < 3))
- //反转集合
- println(List(1, 2, 3).reverse)
- //slice==>与java的substring类似
- println(List(2, 3, 5, 7) slice (1, 4))
- //排序sortBy
- println(List("apple", "to","a","ab") sortBy (_.size) )
- //排序原生值
- println(List("apple", "to","bag","bbc","one").sorted)
- //splitAt值拆分一个list,根据下标的位置
- println(List(2, 3, 5, 7,99,45) splitAt 3)
- //提取第n个元素之前数据作为一个新的集合
- println(List(2, 3, 5, 7, 11, 13) take 2)
- //合并两个集合,进入一个大的集合
- println(List(1, 2) zip List("a", "b") )
- //删除前n个元素后,新生成一个集合
- println(List('a', 'b', 'c', 'd') drop 1)
- //判断两个集合是否相等
- println(List(1, 2) == List(1, 2))
- //合并两个集合 ++
- println(List(1, 2) ++ Set(3, 4, 3) )
- //合并两个集合
- println(List(1, 2) ::: List(2, 3))
- //添加一个元素
- println(1 :: 2 :: Nil)
- //添加一个元素
- println(List(1,6).:+(5))
- //遍历每一个元素,收集case匹配上的元素值,返回一个新的集合
- println(List(0, 1, 0) collect {case 1 => "ok"})
- //将一个元素,根据特定符号,拆分成单个元素组成的集合
- println(List("milk,tea") flatMap (_.split(',')))
- //最大值
- println(List(41, 59, 26).max)
- //最小值
- println(List(10.9, 32.5, 4.23, 5.67).min)
- //连乘
- println(List(5, 6, 10).product)
- //求和
- println(List(11.3, 23.5, 7.2).sum )
- //是否存在
- println(List(34, 29, 18) contains 29 )
- //是否以xxx结束
- println(List(0, 4, 3) endsWith List(4, 3))
- //是否以xxx开始
- println(List(0, 4, 3) startsWith List(0) )
- //最少有一个值小于18即为true
- println(List(24, 17, 32) exists (_ < 18))
- println("=======================================")
- //必须得所有值小于18才返回true
- println(List(5, 17, 2) forall (_ < 18) )
- //求和
- println(List(4, 5, 6).fold(8)(_ + _))
- //求和
- println(List(4, 5, 6).foldLeft(10)(_ + _))
- //求和
- println(List(4, 5, 6).foldRight(0)(_ + _) )
- //求和
- println(List(4, 5, 6).reduce(_ + _) )
- //求和
- println(List(4, 5, 6).reduceLeft(_ + _) )
- //求和
- println(List(4, 5, 6).reduceRight(_ + _) )
- //阶段求和
- println(List(4, 5, 6).scan(0)(_ + _))
- //阶段求和
- println(List(4, 5, 6).scanLeft(0)(_ + _))
- //阶段求和
- println(List(4, 5, 6).scanRight(0)(_ + _))
- //集合转换字符串
- println(List(24, 99, 104).mkString(", "))
- //转成ArrayBuffer
- println(List('f', 't').toBuffer)
- //转Map to List
- println(Map("a" -> 1, "b" -> 2).toList )
- //转Set to Map
- println(Set(1 -> true, 3 -> true).toMap)
- //转List to Set
- println(List(2, 5, 5, 3, 2).toSet)
- //转List to String
- println(List(2, 5, 5, 3, 2).toString)
- //Java 与 Scala 集合互转
- //导入包
- import collection.JavaConverters._
- import collection.JavaConversions._
- import java.util.ArrayList;
- // scala 转 java
- val ja =List(1,5,3).asJava
- println(List(1,5,3))
- println(ja)
- // java 转 scala
- val s =new java.util.ArrayList(3).asScala;
- //集合的模式匹配
- val statuses = List(500, 404)
- val msg = statuses.head match {
- case x if x < 500 => "okay"
- case _ => "whoah, an error"
- }
- println("信息:",msg)
- val msg2 = statuses match {
- case x if x contains(500) => "has error"
- case _ => "okay"
- }
- println("信息:",msg2)
- val msg3 = statuses match {
- case List(404, 500) => "not found & error"
- case List(500, 404) => "error & not found"
- case List(200, 200) => "okay"
- case _ => "not sure what happened"
- }
- println("信息:",msg3)
- val msg4 = statuses match {
- case List(500, x) => s"Error followed by $x"
- case List(e, x) => s"$e was followed by $x"
- }
- println("信息:",msg4)
- val head = List('r','g','b') match {
- case x :: xs => x
- case Nil => ' '
- }
- println(head)
- val code = ('h', 204, true) match {
- case (_, _, false) => 501
- case ('c', _, true) => 302
- case ('h', x, true) => x
- case (c, x, true) => {
- println(s"Did not expect code $c")
- x
- }
- }
- println(code)
- }
顺序输出结果如下:
- (List(3, 5, 4), ,11)
- 红色
- false
- (red,红色)
- (green,绿色)
- List(3, 2, 4, 5)
- List(33, 64)
- List(1, 2, 3, 4)
- (List(1, 2),List(3, 4, 5))
- List(3, 2, 1)
- List(3, 5, 7)
- List(a, to, ab, apple)
- List(apple, bag, bbc, one, to)
- (List(2, 3, 5),List(7, 99, 45))
- List(2, 3)
- List((1,a), (2,b))
- List(b, c, d)
- true
- List(1, 2, 3, 4)
- List(1, 2, 2, 3)
- List(1, 2)
- List(1, 6, 5)
- List(ok)
- List(milk, tea)
- 59
- 4.23
- 300
- 42.0
- true
- true
- true
- true
- =======================================
- true
- 23
- 25
- 15
- 15
- 15
- 15
- List(0, 4, 9, 15)
- List(0, 4, 9, 15)
- List(15, 11, 6, 0)
- 24, 99, 104
- ArrayBuffer(f, t)
- List((a,1), (b,2))
- Map(1 -> true, 3 -> true)
- Set(2, 5, 3)
- List(2, 5, 5, 3, 2)
- List(1, 5, 3)
- [1, 5, 3]
- (信息:,whoah, an error)
- (信息:,has error)
- (信息:,error & not found)
- (信息:,Error followed by 404)
- r
- 204
- Process finished with exit code 0
Scala集合笔记的更多相关文章
- Scala 学习笔记之集合(3)
建立一个Java类,为了演示Java集合类型向Scala集合的转换: import java.util.ArrayList; import java.util.List; public class S ...
- scala 学习笔记--集合
1.scala集合的null 是nil 而不是null 2.set的三个方法union,intersect,diff union--合并去重 intersect--交集 diff--a减去(a和b交集 ...
- 【大数据】Scala学习笔记
第 1 章 scala的概述1 1.1 学习sdala的原因 1 1.2 Scala语言诞生小故事 1 1.3 Scala 和 Java 以及 jvm 的关系分析图 2 1.4 Scala语言的特点 ...
- 基于.net的分布式系统限流组件 C# DataGridView绑定List对象时,利用BindingList来实现增删查改 .net中ThreadPool与Task的认识总结 C# 排序技术研究与对比 基于.net的通用内存缓存模型组件 Scala学习笔记:重要语法特性
基于.net的分布式系统限流组件 在互联网应用中,流量洪峰是常有的事情.在应对流量洪峰时,通用的处理模式一般有排队.限流,这样可以非常直接有效的保护系统,防止系统被打爆.另外,通过限流技术手段,可 ...
- Scala编程 笔记
date: 2019-08-07 11:15:00 updated: 2019-11-25 20:00:00 Scala编程 笔记 1. makeRDD 和 parallelize 生成 RDD de ...
- Scala集合操作
大数据技术是数据的集合以及对数据集合的操作技术的统称,具体来说: 1.数据集合:会涉及数据的搜集.存储等,搜集会有很多技术,存储技术现在比较经典方案是使用Hadoop,不过也很多方案采用Kafka. ...
- Spark:scala集合转化为DS/DF
scala集合转化为DS/DF case class TestPerson(name: String, age: Long, salary: Double) val tom = TestPerson( ...
- Scala语言笔记 - 第一篇
目录 Scala语言笔记 - 第一篇 1 基本类型和循环的使用 2 String相关 3 模式匹配相关 4 class相关 5 函数调用相关 Scala语言笔记 - 第一篇 最近研究了下scala ...
- Scala集合常用方法解析
Java 集合 : 数据的容器,可以在内部容纳数据 List : 有序,可重复的 Set : 无序,不可重复 Map : 无序,存储K-V键值对,key不可重复 scala 集合 : 可变集合( ...
随机推荐
- noi.openjudge 2.6.162 Post Office
http://noi.openjudge.cn/ch0206/162/ 总时间限制: 1000ms 内存限制: 65536kB 描述 There is a straight highway wit ...
- vs2010中使用 git
在没有使用git之前的,我很苦恼.因为我的代码有时在办公室做,有时也带回家做.做了一些时间,放在哪,要用的时间就不知道家里的还是办公室的是新版本了.甚至出现了旧版本把新版本覆盖的乌龙事情.有了git只 ...
- CalISBN.java
/****************************************************************************** * Compilation: javac ...
- js jquery获取当前元素的兄弟级 上一个 下一个元素
原博地址:http://www.jb51.net/article/71782.htm var chils= s.childNodes; //得到s的全部子节点 var par=s.parentNod ...
- CM记录-操作系统调优
1.避免使用swap分区---将hadoop守护进程的数据交换到磁盘的行为可能会导致操作超时:物理内存(交换)--Swap分区 2.调整内存分配策略---操作系统内核根据vm.overcommit_m ...
- Nginx实现数据库端口转发
前言 因开发.测试.生成等服务器网络策略问题,导致部分服务器A需要访问数据库而无法正常访问数据库,此处采用端口代理方式解决此问题,即通过一台能正常访问数据库的服务器B做tcp端口代理,实现服务器A通过 ...
- This page is about building Firefox Desktop
This page is about building Firefox Desktop The Mozilla build system, like the rest of the Mozilla c ...
- luogu 1030 求先序遍历
此题给出中序遍历和后序遍历后的序列,乍一看确乎想不出法子解决,毕竟是逆向思维: 但是后序遍历的特殊性质 son1,son2,x 使得后序的末尾便是根节点,再由中序天然的递归性质便可递归输出,用下fin ...
- Java EE之Struts2路径访问小结
一.项目WEB视图结构 注释:struts.xml:最普通配置,任何无特殊配置 二.访问页面 1.访问root.jsp //方式1: http://localhost/demo/root.jsp // ...
- MGR架构~单写模式架构的搭建
一 简介 :MGR一直没有时间测试,今天咱们来初步了解搭建一下呗 二 环境: mysql5.7.20 单台机器 启动三实例 三 mysql 搭建: 1 建立相关目录+ mkdir -p /data ...