当前位置: 首页 > news >正文

网站如何在百度上做推广/网站设计公司

网站如何在百度上做推广,网站设计公司,莱芜都市网房产,叮咚影视在线观看免费完整版本节主要内容 mutable、immutable集合Set操作实战Map操作实战Tuple操作实战队列操作实战栈操作实战mutable、immutable集合 以下内容来源于scala官方文档: http://www.scala-lang.org/docu/files/collections-api/collections.html Scala collections systematicall…

本节主要内容

  1. mutable、immutable集合
  2. Set操作实战
  3. Map操作实战
  4. Tuple操作实战
  5. 队列操作实战
  6. 栈操作实战

mutable、immutable集合

以下内容来源于scala官方文档:
http://www.scala-lang.org/docu/files/collections-api/collections.html

Scala collections systematically distinguish between mutable and immutable collections. A mutable collection can be updated or extended in place. 
This means you can change, add, or remove elements of a collection as a side effect. Immutable collections, by contrast, never change. You have still operations that simulate additions, removals, or updates, 
but those operations will in each case return a new collection and leave the old collection unchanged.
//大致意思是:scala中的集合分为两种,一种是可变的集合,另一种是不可变的集合
//可变的集合可以更新或修改,添加、删除、修改元素将作用于原集合
//不可变集合一量被创建,便不能被改变,添加、删除、更新操作返回的是新的集合,老集合保持不变

scala中所有的集合都来自于scala.collection包及其子包mutable, immutable当中

//scala.collection.immutable包中的集合绝对是不可变的,函数式编程语言推崇使用immutable集合
A collection in package scala.collection.immutable is guaranteed to be immutable for everyone. Such a collection will never change after it is created.
Therefore, you can rely on the fact that accessing the same collection value repeatedly at different points in time will always yield a collection with the same elements.
//scala.collection.immutable包中的集合在是可变的,使用的时候必须明白集合何时发生变化
A collection in package scala.collection.mutable is known to have some operations that change the collection in place. 
So dealing with mutable collection means you need to understand which code changes which collection when.//scala.collection中的集合要么是mutalbe的,要么是immutable的
//同时该包中也定义了immutable及mutable集合的接口
A collection in package scala.collection can be either mutable or immutable. For instance,
collection.IndexedSeq[T] is a superclass of both collection.immutable.IndexedSeq[T] and collection.mutable.IndexedSeq[T] Generally, 
the root collections in package scala.collection define the same interface as the immutable collections, 
and the mutable collections in package scala.collection.mutable typically add some side-effecting modification operations to this immutable interface.

在scala中,默认使用的都是immutable集合,如果要使用mutable集合,需要在程序中引入

import scala.collection.mutable
//由于immutable是默认导入的,因此要使用mutable中的集合的话
//使用如下语句
scala> val mutableSet=mutable.Set(1,2,3)
mutableSet: scala.collection.mutable.Set[Int] = Set(1, 2, 3)
//不指定的话,创建的是immutable 集合
scala> val mutableSet=Set(1,2,3)
mutableSet: scala.collection.immutable.Set[Int] = Set(1, 2, 3)

直接使用Set(1,2,3)创建的是immutable集合,这是因为当你不引入任何包的时候,scala会默认导入以几个包:
这里写图片描述

Predef对象中包含了Set、Map等的定义
这里写图片描述

scala集合类的层次结构:

scala.collection包中的集合类层次结构如下图:
这里写图片描述
These are all high-level abstract classes or traits, which generally have mutable as well as immutable implementations.

scala.collection.immutable包中的类层次结构:
这里写图片描述

scala.collection.mutable包中的类层次结构:

这里写图片描述

可变集合与不可变集合对应关系:
这里写图片描述

Set操作实战

1 Set(集)是一种不存在重复元素的集合,它与数学上定义的集合是对应的

//定义一个集合
//这里使用的是mutable
scala> val numsSet=Set(3.0,5)
numsSet: scala.collection.mutable.Set[Double] = Set(5.0, 3.0)//向集中添加一个元素,同前一讲中的列表和数组不一样的是
//,Set在插入元素时并不保元素的顺序
//默认情况下,Set的实现方式是HashSet实现方式,
//集中的元素通过HashCode值进行组织
scala> numsSet+6
res20: scala.collection.mutable.Set[Double] = Set(5.0, 6.0, 3.0)//遍历集
scala> for ( i <- res20 ) println(i)
5.0
6.0
3.0//如果对插入的顺序有着严格的要求,则采用scala.collection.mutalbe.LinkedHashSet来实现
scala> val linkedHashSet=scala.collection.mutable.LinkedHashSet(3.0,5)
linkedHashSet: scala.collection.mutable.LinkedHashSet[Double] = Set(3.0, 5.0)scala> linkedHashSet+6
res26: scala.collection.mutable.LinkedHashSet[Double] = Set(3.0, 5.0, 6.0)

Map操作实战

Map是一种键值对的集合,一般将其翻译为映射

//直接初始化
// ->操作符,左边是key,右边是value
scala> val studentInfo=Map("john" -> 21, "stephen" -> 22,"lucy" -> 20)
studentInfo: scala.collection.immutable.Map[String,Int] = Map(john -> 21, stephe
n -> 22, lucy -> 20)//immutable不可变,它不具有以下操作
scala> studentInfo.clear()
<console>:10: error: value clear is not a member of scala.collection.immutable.M
ap[String,Int]studentInfo.clear()^
//创建可变的Map
scala> val studentInfoMutable=scala.collection.mutable.Map("john" -> 21, "stephe
n" -> 22,"lucy" -> 20)
studentInfoMutable: scala.collection.mutable.Map[String,Int] = Map(john -> 21, l
ucy -> 20, stephen -> 22)
//mutable Map可变,比如可以将其内容清空
scala> studentInfoMutable.clear()scala> studentInfoMutable
res3: scala.collection.mutable.Map[String,Int] = Map()//遍历操作1
scala> for( i <- studentInfoMutable ) println(i)
(john,21)
(lucy,20)
(stephen,22)//遍历操作2
scala> studentInfoMutable.foreach(e=>
{val (k,v)=e; println(k+":"+v)}
)
john:21
lucy:20
stephen:22
//遍历操作3
scala> studentInfoMutable.foreach(e=> println(e._1+":"+e._2))
john:21
lucy:20
stephen:22//定义一个空的Map
scala> val xMap=new scala.collection.mutable.HashMap[String,Int]()
xMap: scala.collection.mutable.HashMap[String,Int] = Map()//往里面填充值
scala> xMap.put("spark",1)
res12: Option[Int] = Nonescala> xMap
res13: scala.collection.mutable.HashMap[String,Int] = Map(spark -> 1)//判断是否包含spark字符串
scala> xMap.contains("spark")
res14: Boolean = true//-> 初始化Map,也可以通过 ("spark",1)这种方式实现(元组的形式)
scala> val xMap=scala.collection.mutable.Map(("spark",1),("hive",1))
xMap: scala.collection.mutable.Map[String,Int] = Map(spark -> 1, hive -> 1)scala> "spark" -> 1
res18: (String, Int) = (spark,1)//获取元素
scala> xMap.get("spark")
res19: Option[Int] = Some(1)scala> xMap.get("SparkSQL")
res20: Option[Int] = None

Option,None,Some类型

Option、None、Some是scala中定义的类型,它们在scala语言中十分常用,因此这三个类型非学重要。
None、Some是Option的子类,它主要解决值为null的问题,在java语言中,对于定义好的HashMap,如果get方法中传入的键不存在,方法会返回null,在编写代码的时候对于null的这种情况通常需要特殊处理,然而在实际中经常会忘记,因此它很容易引起 NullPointerException异常。在Scala语言中通过Option、None、Some这三个类来避免这样的问题,这样做有几个好处,首先是代码可读性更强,当看到Option时,我们自然而然就知道它的值是可选的,然后变量是Option,比如Option[String]的时候,直接使用String的话,编译直接通不过。

前面我们看到:

scala> xMap.get("spark")
res19: Option[Int] = Some(1)

那要怎么才能获取到最终的结果呢,

//通过模式匹配得到最终的结果
scala> def show(x:Option[Int]) =x match{
| case Some(s) => s
| case None => “????”
| }
show: (x: Option[Int])Any

scala> show(xMap.get(“spark”))
res21: Any = 1

scala> show(xMap.get(“sparkSQL”))
res22: Any = ????

元组操作实战

前面我们提到Map是键值对的集合,元组则是不同类型值的聚集

//元组的定义
scala> ("hello","china","beijing")
res23: (String, String, String) = (hello,china,beijing)scala> ("hello","china",1)
res24: (String, String, Int) = (hello,china,1)scala> var tuple=("Hello","China",1)
tuple: (String, String, Int) = (Hello,China,1)//访问元组内容
scala> tuple._1
res25: String = Helloscala> tuple._2
res26: String = Chinascala> tuple._3
res27: Int = 1//通过模式匹配获取元组内容
scala> val (first, second, third)=tuple
first: String = Hello
second: String = China
third: Int = 1

队列操作实战

//immutable queue
scala> var queue=scala.collection.immutable.Queue(1,2,3)
queue: scala.collection.immutable.Queue[Int] = Queue(1, 2, 3)//出队
scala> queue.dequeue
res38: (Int, scala.collection.immutable.Queue[Int]) = (1,Queue(2, 3))//入队
scala> queue.enqueue(4)
res40: scala.collection.immutable.Queue[Int] = Queue(1, 2, 3, 4)//mutable queue
scala> var queue=scala.collection.mutable.Queue(1,2,3,4,5)
queue: scala.collection.mutable.Queue[Int] = Queue(1, 2, 3, 4, 5)//入队操作
scala> queue += 5
res43: scala.collection.mutable.Queue[Int] = Queue(1, 2, 3, 4, 5, 5)//集合方式
scala> queue ++= List(6,7,8)
res45: scala.collection.mutable.Queue[Int] = Queue(1, 2, 3, 4, 5, 5, 6, 7, 8)

栈操作实战

//mutable Stack
scala> import scala.collection.mutable.Stack
import scala.collection.mutable.Stack//new 创建方式
scala> val stack = new Stack[Int]
stack: scala.collection.mutable.Stack[Int] = Stack()//Apply创建方式
scala> val stack1=Stack(1,2,3)
stack1: scala.collection.mutable.Stack[Int] = Stack(1, 2, 3)//出栈
scala> stack1.top
res55: Int = 1//入栈
scala> stack.push(1)
res57: stack.type = Stack(1)
//入栈
scala> stack.push(2)
res58: stack.type = Stack(2, 1)
//出栈
scala> stack.top
res59: Int = 2scala> stack
res60: scala.collection.mutable.Stack[Int] = Stack(2, 1)

添加公众微信号,可以了解更多最新Spark、Scala相关技术资讯
这里写图片描述

http://www.lbrq.cn/news/942931.html

相关文章:

  • 一个网站建设大概需要多少费用/推广平台app
  • 泰安城乡建设委员会的网站/买了500元黑科技引流靠谱吗
  • 武汉做网站 古凡/网络营销包括哪些
  • 黑龙江门户网站建设/seo排名培训
  • SEO案例网站建设公司/游戏推广一个月能拿多少钱
  • 做任务网站建设/关键词排名代做
  • 彩票网站怎么做系统/今日头条新闻10条简短
  • 南宁市兴宁区建设局网站/怎么申请网站
  • 做彩网站/福建百度开户
  • 网站建设的系统简介/夫唯老师seo
  • 行业协会网站模板/关键词排名点击软件怎样
  • 做建筑看那些网站/seo谷歌外贸推广
  • 做网站常用的jquery/网络营销的特征和功能
  • wordpress二次开发赚钱/河南网站优化公司哪家好
  • 网站建设有什么优点/搜索引擎优化排名优化培训
  • 长治市郊区住房建设局网站/windows优化大师有哪些功能
  • 做网站好公司哪家好/互联网优化
  • 苏州做学校网站的/淘宝运营培训机构
  • 建立局域网网站/关键词百度云
  • 泰安网站建设哪家专业/怎么做好网络推广销售
  • 西安网站制作计划/网奇seo赚钱培训
  • 怎么用ps做网站ui/上海优化公司有哪些
  • 免费空间有哪些/徐州网站建设方案优化
  • 优秀专题网站/阿里巴巴国际贸易网站
  • 品牌建站/旺道seo优化
  • b2c网站的经营情况/免费一键搭建网站
  • 做b2b网站服务器空间多大的容量/外包公司是正规公司吗
  • 哪个网站可以做全网推广/网络营销策划方案
  • 找别人做网站要注意什么软件/网址大全百度
  • 西安那些做网站的公司/免费宣传平台有哪些
  • 磁悬浮轴承转子不平衡质量控制策略设计:原理、分析与智能实现
  • 栈----3.字符串解码
  • 服务器之光:Nginx--核心配置详解及演练
  • 数据结构预备知识
  • 【RK3568 PWM 子系统(SG90)驱动开发详解】
  • Kafka运维实战 15 - kafka 重设消费者组位移入门和实战【实战】