Spark Big Data Analysis and Practical Notes (Chapter 1 Scala Language Basics-3)

1.3 Scala data structures

For every programming language, array (Array) is one of the important data structures, mainly used to store elements of the same data type. Arrays in Scala are divided into fixed-length arrays and variable-length arrays. To define a fixed-length array, you need to use the new keyword. When defining a variable-length array, you need to import the package import scala.collection.mutable.ArrayBuffer.

1.3.1 Arrays

Array (Array) is mainly used to store data type is each element.

  • Array definition and use
    Arrays in Scala are divided into fixed-length arrays and variable-length arrays. These two arrays are defined as follows:

  • Define a fixed-length array
    New Array[T] (array length) //Define a fixed-length array

  • Define variable-length array
    ArrayBuffer T //Define variable-length array

Note: To define a fixed-length array, you need to use the new keyword, and to define a variable-length array, you need to import the package import scala.collection.mutable.ArrayBuffer.

Below, an example is used to demonstrate the simple use of Scala arrays, and the specific code is as follows.

package cn.itcast.scala

import scala.collection.mutable.ArrayBuffer

object Ch01_ArrayDemo {

  def main(args: Array[String]): Unit = {

    //定义定长数组:定义长度为8的定长数组,数组中的每个元素初始化为0

    val arr1 = new Array[Int](8)

    //打印数组,内容是数组的hashcode值

    println(arr1)

//定义变长数组(数组缓冲),需要导入包:import scala.collection.mutable.ArrayBuffer

    val arr2 = ArrayBuffer[Int]()

    //向变长数组中追加元素

    arr2 += 1

    //打印变长数组

    println(arr2)

    //向变长数组中追加多个元素

    arr2 += (2,3,4,5)

    println(arr2)

    //追加一个定长数组

    arr2 ++= Array(6,7)

    println(arr2)

    //追加一个变长数组(数组缓冲)

    arr2 ++= ArrayBuffer(8,9)

    println(arr2)

    //在变长数组的某个位置插入元素

    arr2.insert(0,-1,0)

    println(arr2)

    //删除数组的某个元素

    arr2.remove(0)

    println(arr2)

  }

}

array traversal

In Scala, if you want to get each element in the array, you need to traverse the array. code show as below:

package cn.itcast.scala

object Ch02_ArrayTraversal {

  def main(args: Array[String]): Unit = {

    //定义定长数组

    //val array = new Array[Int](8) //方式1

    val myArr = Array(1.9, 2.9, 3.4, 3.5) //方式2

    //打印输出数组中的所有元素

    for(x<-myArr){

      print(x+" ")

    }

    println()

    //计算数组中所有元素的和

    var total = 0.0

    for(i <- 0 to (myArr.length-1)){

      total += myArr(i)

    }

    println("总和为:"+total)

    //查找数组中的最大值

    var max = myArr(0)

    for(i <- 1 to (myArr.length-1)){

      if(max < myArr(i)){

        max = myArr(i)

      }

    }

    println("最大值为:"+max)

  }

}

array conversion

Array conversion is to convert the original array through the yield keyword, and a new array will be generated, but the original array remains unchanged. Define an array to generate a new array after taking out even numbers and multiplying them by 10. The specific code is as follows:

package cn.itcast.scala

object Ch03_ArrayYieldTest {

  def main(args: Array[String]): Unit = {

    //定义一个数组

    var arr = Array(1,2,3,4,5,6,7,8,9)

    //将偶数取出乘以10后再生成一个新的数组

    val newArr = for (e <- arr if e%2 == 0) yield e * 10

    println(newArr.toBuffer) //将定长数组转为变长数组

  }

}

1.3.2 Tuples

Scala's tuple is a simple encapsulation of multiple objects of different types. It encloses different values ​​in parentheses and separates them with commas, which means a tuple.

create tuple

The syntax for creating a tuple is as follows:

  val tuple=(元素,元素...)

Below, a simple example is used to demonstrate the creation of tuples, for example: to create a tuple containing String type, Double type and Int type, the specific code is as follows:

scala> val tuple = ("itcast",3.14,65535)

tuple: (String, Double, Int) = (itcast,3.14,65535)

Get the value in the tuple

In Scala, the value in the tuple is obtained by underscore and subscript (for example: tuple._1, tuple._2), and the subscript of the element in the tuple starts from 1. code show as below:

scala> tuple._1                         # 获取第一个值

res0: String = itcast

scala> tuple._2                         # 获取第一个值

res1: Double = 3.14

Zip operation

In Scala's tuples, multiple values ​​can be bound together by using the "zip" command. For example, define two arrays, namely score and names, and bundle these two arrays together. The specific code is as follows:

scala> val scores = Array(88,95,80)

scores: Array[Int] = Array(88, 95, 80)

scala> val names =Array("zhangsan","lisi","wangwu")

names: Array[String] = Array(zhangsan, lisi, wangwu)

scala> names.zip(scores)

res2: Array[(String, Int)] = Array((zhangsan,88), (lisi,95), (wangwu,80))

Note: When the numbers of the two arrays are not equal, the shorter length will be automatically adapted, and the redundant elements will be automatically discarded without corresponding matching elements.

1.3.3 Collections

In Scala, there are three types of collections: List, Set, and Map, all of which extend the Iterable trait.

Scala collections are divided into mutable and immutable collections. Among them, the mutable collection can be updated or extended in place, which means that the collection can be modified, added, and removed elements; the immutable collection class, in contrast, will never change after initialization.

List

In Scala, a List is similar to an array in that all elements of a list have the same type. However, unlike arrays, lists are immutable.

Define different types of lists List, the specific code is as follows:

// 字符串

val fruit: List[String] =List("apples","oranges","pears")

// 整型

val nums: List[Int] = List(1, 2, 3, 4)

// 空

val empty: List[Nothing] = List()

// 二维列表

val dim: List[List[Int]] =

            List(

            List(1, 0, 0),

            List(0, 1, 0),

            List(0, 0, 1)

 )

In Scala, lists can be defined using the "Nil" and "::" operators. code show as below:

// 字符串

val fruit = "apples":: ("oranges":: ("pears" :: Nil))

// 整型

val nums = 1 :: (2 :: (3 :: ( 4 :: Nil)))

// 空列表

val empty = Nil

// 二维列表

val dim = (1 :: (0 :: (0 :: Nil))) ::

                (0 :: (1 :: (0 :: Nil))) ::

                (0 :: (0 :: (1 :: Nil))) :: Nil

Scala also provides many methods for manipulating List, as shown in the following table.
insert image description here
The sample code is as follows:

package cn.itcast.scala

object Ch04_ListTest {

  def main(args: Array[String]): Unit = {

    //定义List集合

    //val fruit:List[String] = List("apples","oranges","pears")

    val fruit = "apples"::("oranges"::("pears"::Nil))

    val nums = Nil

    println("Head of fruit:" + fruit.head)

    println("Tail of fruit:" + fruit.tail)

    println("check if fruit is empty:"+fruit.isEmpty)

    println("check if nums is empty:"+nums.isEmpty)

    println("Take of fruit:"+fruit.take(2))

    println("contains of fruit oranages:"+fruit.contains("oranges"))

  }

}

Set

In Scala, a Set is a collection with no duplicate objects and all elements are unique. By default, Scala uses an immutable Set collection. If you want to use a mutable Set collection, you need to introduce the scala.collection.mutable.Set package.

The syntax for defining a Set collection is as follows:

val set: Set[Int] = Set(1,2,3,4,5)

Scala provides many methods for manipulating Set collections. Next, we list some common methods for operating Set collections, as shown in the following table.
insert image description here
The specific code is as follows:

package cn.itcast.scala

object Ch05_SetTest {

  def main(args: Array[String]): Unit = {

    //定义Set集合

    val site = Set("Itcast","Google","Baidu")

    val nums:Set[Int] = Set()

    println("第一个网站是:"+site.head)

    println("最后一个网站是:"+site.tail)

    println("site集合是否为空:"+site.isEmpty)

    println("nums集合是否为空:"+nums.isEmpty)

    println("查看site集合的前两个网站:"+nums.take(2))

    println("site集合中是不包含网站Google:"+site.contains("Google"))

  }

}

Map

In Scala, Map is an iterable key-value pair (key/value) structure, and the key is unique, but the value is not necessarily unique. All values ​​are obtained through the key. There is a corresponding relationship between the key and the value of all elements in the Map, and this relationship is the mapping. There are two types of Maps, mutable collections and immutable collections. The default is immutable Map. If you need to use a mutable Map collection, you need to introduce the import scala.collection.mutable.Map class.

The syntax for defining a Map collection is as follows:

var A:Map[Char,Int] = Map(键 -> 值,键 -> 值...)  //Map键值对,键为Char,值为Int

Scala also provides many methods for manipulating Map collections. Next, we list some common methods for operating Map collections, as shown in the following table:
insert image description here
The sample code is as follows:

package cn.itcast.scala

object Ch06_MapTest {

  def main(args: Array[String]): Unit = {

    //定义Map集合

    val colors = Map("red"-> "#FF0000",

      "azure"-> "#F0FFFF",

      "peru"-> "#CD853F")

    val peruColor = if(colors.contains("peru")) colors("peru") else 0

    val azureColor = colors.getOrElse("azure",0)

    println("获取colors中键为red的值:"+colors("red"))

    println("获取colors中所有的键:"+colors.keys)

    println("获取colors中所有的值:"+colors.values)

    println("检测colors是否为空:"+colors.isEmpty)

    println("判断colors中是否包含键peru,包含则返回对应的值,否则返回0:"+peruColor)

    println("判断colors中是否包含键azure,包含则返回对应的值,否则返回0:"+azureColor)

  }

}

Reprinted from: https://blog.csdn.net/u014727709/article/details/131679759

Welcome to start, welcome to comment, welcome to correct

Guess you like

Origin blog.csdn.net/u014727709/article/details/131679759