Scala language foundation: one article takes you familiar with six data structures

The data structure foundation of Scala language, covering arrays, lists, tuples, sets, maps, iterators, etc.

1. Array

Arrays are data structures often used in programming, generally including fixed-length arrays and variable-length arrays. This tutorial aims to quickly grasp the most basic and commonly used knowledge, so only fixed-length arrays are introduced.

A fixed-length array is an array with a constant length. It is declared in Scala using Array, as follows:

val intValueArr = new Array[Int](3)  //声明一个长度为3的整型数组,每个数组元素初始化为0
intValueArr(0) = 12 //给第1个数组元素赋值为12
intValueArr(1) = 45  //给第2个数组元素赋值为45
intValueArr(2) = 33 //给第3个数组元素赋值为33

It should be noted that in Scala, the application of array elements uses parentheses, not square brackets. In fact, Scala provides a more concise array declaration and initialization method, as follows:

val intValueArr = Array(12,45,33)
val myStrArr = Array("BigData","Hadoop","Spark")

As can be seen from the above code, there is no need to give the array type, and Scala will automatically infer the array type based on the provided initialization data.

2. List

Below we first declare a list:

val intList = List(1,2,3)

The list has the concept of head and tail, you can use intList.head to get the head 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 new elements to the head of the list to get a new list, as follows:

val intList = List(1,2,3)
val intListOther = 0::intList

Note that after the above operation is executed, intList will not change, it is still List(1,2,3), and intListOther is a new list List(0,1,2,3)

The :: operator is right associative, so if you want to build a list List(1,2,3), you can actually use the following method:

val intList = 1::2::3::Nil

In the above code, Nil represents an empty list.
We can also use the ::: operator to connect different lists to get a new list, for example:

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

3. Tuple

A tuple is an aggregation of values ​​of different types. Unlike lists, tuples must have elements of the same type, whereas tuples can contain elements of different types.
Below we declare a tuple named tuple (in order to see the effect of the declaration, we enter the code in the Scala interpreter and execute it this time):

scala> val tuple = ("BigData",2015,45.0)
tuple: (String, Int, Double) = (BigData,2015,45.0)  //这行是Scala解释器返回的执行结果
scala> println(tuple._1)
BigData
scala> println(tuple._2)
2015
scala> println(tuple._3)
45.0

From the execution effect of the above code in the Scala interpreter, we can see that it is very simple for us to declare a tuple, and we only need to surround the elements of multiple tuples with parentheses.
When it is necessary to access the value of an element in the tuple, it can be achieved in a way like tuple._1, tuple._2, tuple._3.

4. set

A set is a collection of elements that do not repeat. The elements in the list are organized according to the order of insertion, but the elements in the "set" do not record the insertion order of the elements, but organize the value of the elements by the "hash" method, so it allows You find an element quickly.

Sets include mutable sets and immutable sets. By default, immutable sets are created. Usually we use immutable sets.
Below we create an immutable collection in the default way, as follows (executed in the Scala interpreter):

scala> var mySet = Set("Hadoop","Spark")
mySet: scala.collection.immutable.Set[String] = Set(Hadoop, Spark)
scala> mySet += "Scala"  //向mySet中增加新的元素
scala> println(mySet.contains("Scala"))
true

In the above declaration, if val is used, an error will be reported when mySet += "Scala" is executed, so it needs to be declared as var. (If you want to declare a mutable set, you need to introduce the scala.collection.mutable.Set package)

5. Mapping

In Scala, a map (Map) is a collection of key-value pairs, that is, a correspondence between keys and values ​​is established. In a map, all values ​​can be obtained by keys.

Maps include mutable and immutable. By default, an immutable map is created. If you need to create a mutable map, you need to introduce the scala.collection.mutable.Map package.

Below we create an immutable map:

val university = Map("XMU" -> "Xiamen University", "THU" -> "Tsinghua University","PKU"->"Peking University")

If you want to get the value in the map, you can get it by key, as follows:

println(university("XMU"))

The above code can get the value Xiamen University through the key "XMU".
If you want to check whether a value is contained in the map, you can use the contains method, as follows:

val xmu = if (university.contains("XMU")) university("XMU") else 0
println(xmu)

Looping through the map is a frequently used operation. The basic format is:

for ((k,v) <- 映射) 语句块

An example is given below:

for ((k,v) <- university) printf("Code is : %s and name is: %s\n",k,v)

6. Iterators

In Scala, an iterator (Iterator) is not a collection, but provides a way to access the collection. Iterators are great when building a collection is expensive (such as reading all the lines of a file into memory).
Iterators contain two basic operations: next and hasNext. next can return the next element of the iterator, and hasNext is used to detect whether there is a next element.

With these two basic operations, we can smoothly traverse all the elements in the iterator.
Iterators can usually be traversed through a while loop or a for loop.
The while loop is as follows:

val iter = Iterator("Hadoop","Spark","Scala")
while (iter.hasNext) {
    println(iter.next())
}

Note that after the above operations are executed, the iterator will move to the end and can no longer be used. If you continue to execute println(iter.next), an error will be reported. In addition, in the above code, iter.next and iter.next() can be used, but parentheses cannot be added after hasNext.

The for loop is as follows:

val iter = Iterator("Hadoop","Spark","Scala")
for (elem <- iter) {
    println(elem)
}

Reference link:

http://dblab.xmu.edu.cn/blog/spark/

https://www.runoob.com/scala/scala-tutorial.html

History recommendation

Mom no longer has to worry about dual system installation!

Spark Machine Learning: Model Evaluation Metrics

Spark Machine Learning: Frequent Pattern Mining

Reptile combat: Selenium crawls Jingdong products

Reptile combat: Douban movie top250 crawling

Reptile combat: Scrapy framework crawls QQ music

Data Analysis and Mining

Data Structures and Algorithms

Machine Learning and Big Data Components

Welcome to pay attention, thank you for "watching", it's rare to follow the fate~

 

Guess you like

Origin blog.csdn.net/qq_36936730/article/details/106089071