Scala basic syntax

Since learning Spark requires Scala, here are some basic grammars of scala.

be careful:
Scala doesn't need a semicolon at the end of a line

1 variable type

  • val is immutable. It must be initialized at the time of declaration, and it cannot be assigned again after initialization
  • var is variable. It needs to be initialized when declaring. After initialization, it can be assigned again.

2 basic data type

Including Byte, Char, Short, Int, Long, Float, Double and Boolean.

Unlike Java, these types are "classes" in Scala.

3 literal quantity

Including integer literal, floating-point literal, Boolean literal, character literal, string literal, symbol literal, function literal and tuple literal.

Literal quantity can be understood as the value assigned to variable.

Scala allows methods to be executed directly on "literals". For example:

5.toString() //Generate string "5"
"abc".intersect("bcd")  //Output "bc"

4 operator

In Scala, operators such as add (+), subtract (-), multiply (*), divide (/), and remainder (%) can be used, and these operators are methods. For example, 5 + 3 and (5)+ (3) Is equivalent. That is, a. method B is equivalent to a. method (b).

The + + and – operators are not provided in Scala, but can be replaced by + = and - =.

5 Range and for loops

When creating a Range, you need to give the start point, end point and step size of the interval (the default step size is 1).

1 to 5 // Create a numerical sequence from 1 to 5, including the end point of the interval 5 in steps of 1
1 until 5 // Create a numerical sequence from 1 to 5, excluding the interval end point 5, with a step of 1
1 to 10 by 2 // Create a numerical sequence from 1 to 10, including the end point of the interval 10 in steps of 2

When executing the for loop, we often use the numerical sequence. For example, the value of i loops from 1 to 5. At this time, Range can be used to implement it.

The format of for loop statement in Scala is as follows: for (variable < - expression) statement block, in which "variable < - expression" is called "generator"

for (i <- 1 to 5) println(i)
for (i <- 1 to 5 if i%2==0) println(i)

Sometimes, we may want to filter out some results that meet the specified conditions. At this time, we need to use an expression called "guard".

for (i <- 1 to 5 if i%2==0) println(i) // Output all even numbers from 1 to 5

Scala also supports the case of "multiple generators", which can be separated by semicolons (which can be understood as nested loops)

for (i <- 1 to 5; j <- 1 to 3) println(i*j)

6 print statements

print("Hello world!"); // Non wrapping printing
print("Hello world!"); // Line feed printing
printf("My name is %s. I hava %d apples and %d eggs.\n","scala",i,j) // Format printing (similar to C language)

7 array

Generally, it includes fixed length array (constant length) and variable length array (variable length)

val intValueArr = new Array[Int](3)  //Declare an integer array with a length of 3, and initialize each array element to 0
intValueArr(0) = 12 //Assign 12 to the first array element
intValueArr(1) = 45  //Assign 45 to the second array element
intValueArr(2) = 33 //Assign 33 to the third array element

In Scala, references to array elements use parentheses instead of square brackets.
Scala provides more concise array declaration and initialization methods.

val intValueArr = Array(12,45,33) // Automatically infer the type of array based on the provided initialization data

8 list

List has the concept of head and tail. You can use intlist Head to get the header of the list defined above. The value is 1. Use intlist Tail to get the tail of the list defined above. The value is List(2,3). It can be seen that the head is an element, while the tail is still a list.

Since the head of the list is an element, we can use the:: operation to add a new element to the head of the list. The original list remains unchanged and returns to the new list.

val intList = List(1,2,3) // Declare a list
val intListOther = 0::intList

: operators are right combined. Therefore, if you want to build a List(1,2,3), you can also use the following method:

val intList = 1::2::3::Nil // Nil indicates an empty list

We can also use the::: operator to connect different lists to get a new list

val intList1 = List(1,2)
val intList2 = List(3,4)
val intList3 = intList1:::intList2

9 tuple

Tuples are collections of different types of values. Tuples are different from lists. Each element in the list must be of the same type, and tuples can contain elements of different types.

val tuple = ("BigData",2015,45.0) // Declare a tuple (just enclose the elements of multiple tuples with parentheses)
// Access the value of an element in a tuple, tuple_ 1,tuple._2,tuple._ three
println(tuple._1)
println(tuple._2)
println(tuple._3)

10 sets

A set is a collection of non repeating elements. The elements in the list are organized according to the insertion order. However, the elements in the "set" do not record the insertion order of the elements, but organize the values of the elements in the "hash" method. Therefore, it allows you to quickly find an element.

Sets include variable sets and immutable sets. Immutable sets are created by default. We usually use immutable sets.

If you want to declare a mutable set, you need to introduce Scala collection. mutable. Set package.

Although both variable sets and immutable sets have the operation of adding or deleting elements, they are very different. Operating on an immutable set will produce a new set, and the original set will not change. The operation itself is to change the set.

11 map

A map is a collection of key value pairs

Mapping includes variable mapping and immutable mapping. Immutable mapping is created by default. If you need to create variable mapping, you need to introduce Scala collection. mutable. Map package.

// Declare immutable mapping
val university = Map("NEU" -> "Northeastern University", "THU" -> "Tsinghua University","PKU"->"Peking University") 

// Get the value of key value pair through key
println(university("NEU")) 

// Use the contains method to check whether the mapping contains a value
println(if (university.contains("NEU")) university("NEU") else 0) 

What we defined above is immutable mapping. We cannot update the elements in the mapping or add new elements. If you want to update the mapped elements, you need to define a mutable mapping

import scala.collection.mutable.Map
val university2 = Map("NEU" -> "Northeastern University", "THU" -> "Tsinghua University","PKU"->"Peking University")
university2("NEU") = "Northeastern University " //Update the value of an existing element
university2("FZU") = "Fuzhou University" //Add new element

// You can also use the + = action to add new elements
university2 += ("TJU"->"Tianjin University") //Add a new element
university2 += ("SDU"->"Shandong University","WHU"->"Wuhan University") //Add two new elements at the same time

Loop traversal mapping

for ((k,v) <- university) printf("Code is : %s and name is: %s\n",k,v) // Traverse key value pairs
for (k<-university.keys) println(k) // Traversal key
for (v<-university.values) println(v) // Traversal value

Keywords: Scala Spark

Added by fxb9500 on Sun, 06 Mar 2022 10:19:54 +0200