scala entry and related knowledge

Lesson: Getting Started with Scala
1: Scala significant value of
2: Scala basis functions Getting combat
3: Scala function entry-combat
4: Scala in Array, Map, Tuple combat
5: Integrated Spark source code and resolve cases

Scala and Java relationship:
a: they are based on the JVM, but Scala Java can call any of the functions, such as Spark runs on Hadoop, it can call all the functions on Hadoop.
Two: You can think Scala it is an upgraded version of Java, because Scala itself is a support object-oriented language, in Scala, everything is an object, it is a language of pure object-oriented, while Scala is an object-oriented and a combination of functional language.


Big data is a development language Scala, for the following reasons:
a: big data is to calculate the data itself, and Scala that is capable of object-oriented project of the organization, but also to calculate the functional data.
Two: Big Data is now the de facto standard computing framework Spark, which is developed by Scala, because the data is calculated, it is the Scala functional programming, which implements the algorithm is very simple and elegant.
Example: kafka, which is a middleware message, if external data is to flow into a large data center, we have generally used as adapters kafka, large that if the data flow outside the data center, but also by kafka (The data calculated Spark to write to HBASE or MySql, during which we will use kafka), a lot of big data components are used in Scala, SO, if you want to become a top big data development expert, you must master Scala.

Results type conversions:
1.5 * 2 Results RES1: Double = 3.0
res1.toInt // press table can autocomplete

Scala declaration of variables:
a: variable Variable declaration 
var name = "Spark" // declares variable a variable name, the value Spark, value of the name can be changed to Scala: name = "Scala"

II: immutable variables declared
val result = 2 + 10 // declare a variable immutable result and its value is 12, its value can not be changed, if you re-assigned to the result 13, result = 13, this time being given, because the variable result variable is immutable.

Note: the default data Spark RDD are immutable.


III: manually specify the type of variable assignment
val age: Int = 0 // specified age immutable type variables Int, a value of 0
Val name: String name = null // immutable variables specified type String, its default value is null
Note: If you specify a specific type of variable, subsequent to assign values to variables: type can only be assigned or type of subtype you specify.

IV: a plurality of line of code simultaneously declare variables
val age1, age2, age3 = 0

Note: Scala, it can be automatically converted to complete the operation of our so-called basic types and object types in Scala, the basic type is the object, such as: 10.toLong, in Java, there is no basic types of this approach, but Scala in there.

Scala no such ++ 1, 1--, this operator.
For example: var age = 10; age ++ // this operation is wrong, but you can do this: age + = 1

取最小值:
import scala.math._
min(20,4)
结果为4

构建数组:
val array = Array(1,2,3,4)
内部实现:val array = Array.apply(1,2,3,4)

五:if 表达式 
val age = 19
if(age>=18) "adult" else "child"
结果String = adult
//注意:Scala中,if表达式是有结果的,但Java中是没有的。

****最后一行的值是整个代码块的返回值:如下****
var buffered = 0;
val result = if(age>=18){
| "audlt"
| buffered = 10
| buffered
}
//结果result:AnyVal = 10

六:循环
0 to element

for(i <- 0 to element) println(i)

//说明:<- 它是个提取符,提取出这个集合Range中的每一个元素并把它的值赋值给i

for(i <- 0 to element if i%2 ==0) {
println(i)
}
//打印出偶数

如何终止循环,因Scala中没有break语句,但Scala中有一个Breaks对象,我们可以使用。
一:导包:import scala.util.control.Breaks._
二:使用
for(i <- 1 to 10){
if(i==4)
break
println(i)
}
结果1 2 3 
scala.util.control.BreakControl
//其实此时已经实现了return

七:函数定义:
默认参数
def f3(param1:String,param2:Int=30)=param1+param2
f3("Spark")
值:Spark30

不按顺序传参:带名参数,如下
f3(param2=100,param1="Scala")
结果:Scala100

变长参数函数
def sum(numbers : Int*)={var result=0;for(element <- number) result+=element;result}
sum(1,2,3,4,5,6,7,8,9,10)
值为55

sum(1 to 100:_*) 值为5050,:_*表示将里面的每个元素提取出来,再相加
没有返回值的函数叫做过程。


八:lazy:如果一个变量声明为lazy,表示这个变量只有在第一次使用时,才会发生计算,比如说打开文件,打开数据库,操作网络,如果将变量声明为lazy,特别有用。

九:异常
try{

}catch{

}finally{
println("finally")
}

//注意:与Java中的异常稍微不一样。


十:集合操作
创建不可变数组:val arr = new Array[Int](5) //创建了一个不可变的数组,大小为5,官方说法:val表示arr本身的对象不可修改,但对象的内容是可以修改的
访问元素:arr(3) //访问第4个元素。
修改元素的值:arr(2)= 8


创建不可变数组时并赋值:val arr1 = Array("Scala","Spark")
其实它内部用的是apply: val arr1 = Array.apply("Scala","Spark") // apply就是内部工厂方法构造器


创建可变数组:可增加或删减元素
一:导包 import scala.collection.mutable.ArrayBuffer
二:val arrBuffer = ArrayBuffer[Int]()
追加元素:arrBuffer +=10;
arrBuffer +=(11,1,3,4,5) //那么这个可变数组就有5个元素
arrBuffer ++=(1,2,3,4) //在现有的arrBuffer的5个元素的后面追加1,2,3,4 四个元素

删减元素:
arrBuffer.trimEnd(3) //截断arrBuffer后面的3个元素

固定位置加元素:arrBuffer.insert(5,100),第5个元素增加100这个元素,那么后面的元素会往后移
固定位置删元素:arrBuffer.remove(5),删除第5个位置的元素
arrBuffer.remove(5,3),第5个位置开始删除3个元素,包括5的位置

for循环遍历数组
for(element <- arrBuffer)
{
println(element)
}
数组求和:arrBuffer.sum
数组求最大值:arrBuffer.max
数组排序:scala.util.Sorting.quickSort(arrBuffer)

Map
定义不可变的Map val persons = Map("Spark" ->6,"Hadoop" -> 11)
访问key为Hadoop的Map里面的值:persons("Hadoop")

定义可变的Map
val persons = scale.collection.mutable.Map("Spark" ->6,"Hadoop" -> 11)
加元素:
persons=+("Flink" -> 5)
减元素:
persons-="Flink"

Map中的if()else()操作的变型
在Spark中写法是:persons.getOrElse("Spark",1000) //如果persons这个Map中包含有Spark,取出它的值,如果没有,值就是1000。

for循环访问persons里面的Key和value
for((key,value)<- persons)
println(key+":"+value)
值:Hadoop:11
Spark:6

创建可排序的Map:会对Map的key进行排序
val persons = scale.collection.immutable.SortedMap("Spark" ->6,"Hadoop" -> 11)

tuple:它可以存放很多的不同类型的数据
val tuple=("Spark",6,99.0)
访问:tuple._1 结果String=Spark
注:它的访问下标是从1开始的,跟数组不一样。

 

开发环境:

  Ubuntu 14.x

  Scala 2.10.4 

  Java 1.8

 

移除一个数组中第一个负数以后的负数

实现方式一:

      

上述第一种实现方式由于在发现第一个负数之后的每一个负数会立即进行移除,性能较差,多次移动数组

实现方式二:

上述的方式是首先记录所有不需要移除的元素的索引,最后一次性移除所有需要移除的元素,性能相对较高

Guess you like

Origin blog.csdn.net/baibaichenchen/article/details/74352092