当前位置:网站首页>Spark foundation -scala

Spark foundation -scala

2022-07-06 19:22:00 LackyQueen

scala characteristic

Object oriented , Functional programming , Static type , Extensible , Interoperable

idea Download and install from scala plug-in unit

open idea-->plugins--> Search for scala--> Click on installed install

establish scala Program

Get into New Project:

 

 

  Select the corresponding jdk and scala-sdk Create :

 

 

  After the creation is successful, you can see the directory :

 

 

  We are src Write code in the file .

scala Basic grammar

1. Declare values and variables

Use var Declared variables , The value is variable .

Use val Declared variables , Also called constant , Value is immutable .

var myVar:String = "Hello"
val age:Int = 10

Be careful

(1)scala Variables in must be initialized when declared .

(2) When variables are declared , We can not give the type of variable , During initialization ,scala The type inference mechanism of can automatically calculate the value of variable initialization .

var myVar = "Hello"
val age = 10

(3) Use keywords var or val When variables are declared , The following variable name cannot be the same as scala Duplicate names of reserved words in , And variable names can start with letters or underscores , And variable names are strictly case sensitive .

2. data type

 

 

 

AnyVal: Value type , The data described by the value type is a non empty value , Not an object . It predefines 9 Types , among ,Unit Is a value type that does not represent any meaning , It works on void similar .

AnyRef: Indicates the type of application , It can be said that , Except value , All types inherit from AnyRef.

Nothing: Subtypes of all types , Also known as bottom type . Its common use is to send a termination signal , For example, throw an exception 、 Program exit or infinite loop .

Null: Forehead subtypes of all application types , Its main use is with other JVM Language operation , Hardly scala The code uses .

3. Arithmetic and operator overloading

scala Arithmetic operators in (+、-、*、/、%) Function and java It's the same , Bit operators (&、|、>>、<<) It's the same . A special emphasis on ,scala These operators of are actually methods .

val a = 1
val b = 2
a+b
a.+(b)

scala No operator is provided ++ and --. If we want to achieve the effect of increasing or decreasing , have access to +=1 or -=1 This way, .

4. Control structure statement

stay scala in , Control structure statements include Conditional branch statement and Loop statement .

Conditional branch statement

//if...
if( Boolean expression ){
     The result is ture, Executed statements 
}

//if...else...
if( Boolean expression ){
     The result is ture, Executed statements 
}else{
     The result is false, Executed statements 
}

if...else if ..else..
if( Boolean expression 1){
     Boolean expression 1 by ture, Executed statements 
}else if( Boolean expression 2){
     Boolean expression 2 by ture, Executed statements 
}else if( Boolean expression 3){
     Boolean expression 3 by ture, Executed statements 
}else{
     The above results are false, Executed statements 
}

// nesting 
if( Boolean expression 1){
     Boolean expression 1 by ture, Executed statements 
    if( Boolean expression 2){
         Boolean expression 2 by ture, Executed statements 
    }
}else if( Boolean expression 3){
     Boolean expression 3 by ture, Executed statements 
    else if( Boolean expression 4){
         Boolean expression 4 by ture, Executed statements 
    }
}else{
     All above false, Execute statement 
}

Loop statement

scala Medium for Loop statements and java Medium for Circular statements are quite different in syntax .

for loop The statement format of the statement is as follows :

for ( Variable <- expression / Array / aggregate ){
     Loop statement ;
}

from 0 Loop to 9, After each cycle, the value will be printed out for operation demonstration , stay scala In the syntax , We can just use “0 to 9” Express , The scope includes 9, The code is as follows :

for (i <- 0 to 9){
    print(i+" ")
}

scala stay for Copper drums can be used in circular statements if Judge statements filter some elements , Multiple filter conditions are separated by sorting . Such as , Input 0-9 The range is greater than 5 An even number of , The code is as follows :

for (i <- 0 to 9; if i%2==0; if i>5){
    print(i+" ")
}

Demonstrate by printing out odd numbers while loop Use . Suppose there is a variable x=1, Judge whether it is less than 10, If yes, print out , Then it's going on +2 operation . The code is as follows :

 var x = 1while(x < 10){  print(x+" ")  x += 2}

do...while loop The syntax format of the statement is as follows :

do {
     Loop statement ;
}while( Boolean expression )

do...while Circular statements and while The main difference between circular statements is ,do...while Statement is executed at least once , The code is as follows :

x = 10
do{
    print(x+" ")
    x += 1
}while (x < 20)

5. Methods and functions

scala The method of is part of the class , A function is an object that can be assigned to a variable .scala Can be used in def Statement and val Statement definition function , The definition method can only use def sentence .

Method

scala The definition format of the method is as follows :

def functionName([ parameter list ]):[return type]={
    function body
    return [expr]
}

below , Define a method add, Add two numbers together and , The code is as follows :

def add(a: Int, b: Int): Int={
    var sum:Int = 0
    sum = a + b
    return sum
}

scala The format of method call is as follows :

// There is no object call format using instances 
functionName( parameter list )

// Method uses the object of the instance to call , We can use similar java The format of ( Use “.” Number )
[instance.]functionName( parameter list )

below , In the class Test in , Define a method addInt, Realize the addition and summation of two integers . ad locum , We go through “ Class name . Method name ( parameter list )” To make the call , The code is as follows :

:paste            // Enter multi line input mode 
object Test{
    def addInt(a: Int, b: Int): Int={
        var sum: Int = 0
        sum = a + b
        retrun sum
    }
}

ctrl+D  // Exit multi line input mode 

Test.addInt(4,5)
val addInt = (a: Int, b: Int) => a + b

(1) Method into a function

Method is converted into a function in the following format :

val f1 = m _

Method name m Followed by a space and an underscore , Is to tell the compiler to put the method m Convert to function , Instead of calling this method . below , Define a method m, Implement the method m Convert to function , The code is as follows :

def m(x: Int, y: Int): Int = x + y  // Method 

val f = m _  // function 

Be careful

scala The return value type of the method can be left blank , The compiler can automatically infer , But for recursive methods , Return type must be specified .

scala data structure

scala Provides many data structures , Such as the common array 、 Tuples 、 Collection etc. .

1. Array

Array (Array) It is mainly used to store elements with the same data type

1.1 Array definition and use

scala The array in is divided into Fixed length array and Variable length array , These two arrays are defined as follows :

new Array[T] ( The length of the array )   // Fixed length array 
ArrayBuffer[T]()    // Variable length array 

Be careful : When defining a variable length array , You need to guide the package import scala.collection.mutable.ArrayBuffer

below , Let's demonstrate with examples scala Arrays are easy to use , The specific code is as follows :

file ArrayDemo.scala:

import scala.colletion.mutable.ArrayBuffer
object ArrayDemo{
    def main(array: Array[String]) {
        //import scala.collection.mutable.ArrayBuffer
object ArrayDemo {
  def main(args: Array[String]): Unit = {
    //  Define fixed length arrays : Define the length as 8 A fixed length array of , Each element in the array is initialized to 0
    val arr1 = new Array[Int](8)

    // Print fixed length array , The content is the array hashcode value 
    println(arr1)

    // Define the side length array ( Array buffer ), Package import required 
    val ab = ArrayBuffer[Int]()

    // Append elements to the variable length array 
    ab += 1
    // Print variable length arrays 
    println(ab)

    // Append multiple elements to the variable length array 
    ab += (2,3,4,5)
    println(ab)

    // Append a fixed length array 
    ab ++= Array(6,7)
    println(ab)

    // Append a variable length array ( Array buffer )
    ab ++= ArrayBuffer(8,9)
    println(ab)

    // Insert an element somewhere in a variable length array 
    ab.insert(0,-1,0)  // stay 0 Index position insert -1 and 0
    println(ab)

    // Delete an element of an array 
    ab.remove(0)
    println(ab)
  }
}

1.2 Traversal of array

scala in , If you want to get every element in the array , You need to traverse the array

file ArrayTraversal.scala:

object ArrayTraversal {
  def main(args: Array[String]): Unit = {
    // Define fixed length arrays 
    //val array = new Array[Int](8)  // The first way 

    val myArr = Array(1.9, 2.9, 3.4, 3.5) // The second way 

    // Print out all elements in the array 
    for (x <- myArr){
      print(x+" ")
    }
    // Print line breaks 
    println()

    // Calculates the sum of all elements in the array 
    var total = 0.0
    for (i <- 0 to (myArr.length - 1)){
      total += myArr(i)
    }
    println(" Sum to :"+total)

    // Find the largest element in the array 
    var max = myArr(0)
    for (i <- 1 to (myArr.length - 1)){
      if (myArr(i) > max){
        max = myArr(i)
      }
    }
    println(" Maximum :" + max)
  }
}

1.3 Array conversion

Array conversion is through yield Keyword to convert the original array , A new array will be generated , However, the original array remains unchanged . Define an array , The implementation takes out the even number and multiplies 10 Generate a new array , The code is as follows :

file ArrayYieldTest.scala:

object ArrayYieldTest {
  def main(args: Array[String]): Unit = {
    // Define an array 
    val arr = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)
    val newArr = for (e <- arr; if e % 2 ==0) yield e * 10
    println(newArr.toBuffer)  // Convert fixed length array to variable length array output 
  }
}

2. Tuples

scala Tuples of are a simple encapsulation of multiple objects of different types , It encloses different values in parentheses , And separated by commas , That is, tuples .

2.1 Creating a tuple

The syntax for creating tuples is as follows :

val tuple = ( Elements , Elements ...)

Create a containing String type ,Double Type and int Tuples of type , The code is as follows :

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

2.2 Gets the value in the tuple

stay scala in , The value of tuple song is obtained by underlining and footmark , The element footmark in the tuple is from 1 At the beginning .

tuple._1   // Get the first value 
tuple._2   // Get the second value 

3.2 Zipper operation

stay scala Tuple , By using “zip” Commands bind multiple values together . Such as , Define two arrays , Namely scores and names, Bundle these two arrays together , The code is as follows :

val scores = Array(88,95,80)
val names = Array("zhangsan ","lisi","wangwu")
names.zip(scores)

3. aggregate

scala in , There are three main categories of collections :List、Set as well as Map

scala The set is divided into variable (mutable) And immutable (immutable) Set .

3.1 List

stay scala in ,List Lists are similar to arrays , All elements of the list have the same type . there list The default is immutable list , If you want to define a variable list , Import required “impport scala.collection.mutabe.ListBuffer” package .

Define different types of lists list, The code is as follows :

val = fruit:List[String] = List("apples","oranges","pears")   // character string 
val nums:List[Int] = List(1, 2, 3, 4)  // integer 
val empty:List[Nothing] = List()  // empty 
val dim:List[List[Int]] = List(List(1,0,0),
                               List(0,1,0),
                               List(0,0,1))

stay scala in , have access to "Nil" and "::" Operator to define the list .

val fruit = "apples"::("oranges"::("pears"::Nil))   // character string 
val nums = 1::(2::(3::(4::Nil)))   // integer 
val empty = Nil
val dim = (1::(0::(0::Nil))) ::
          (0::(1::(0::Nil))) ::
          (0::(0::(1::Nil))) :: Nil

scala It also provides many operations List Methods :

Method name Related instructions
head Get the first element of the list
tail Returns a list of all elements except the first
isEmpty If the list is empty , Then return to ture, Otherwise return to false
take Before getting the list n Elements
contains Determines whether the specified element is included

Define a fruit list , Use common methods to deal with thunderstorms fruit Carry out relevant operations , The code is as follows :

file ListTest.scala:

object ListTest {
  def main(args: Array[String]): Unit = {
    // Definition List aggregate 
//    val fruit2:List[String] = List("apples","oranges","pears")
    val fruit = "apples"::("oranges"::("pears"::Nil))
    val nums = Nil  // empty List aggregate 

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

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

    println("Tack of fruit:" + fruit.take(2))
    println("Contains of fruit:" + fruit.contains("apples"))
  }
}

3.2.Set

stay scala in ,Set Is a collection without duplicate objects , All elements are unique . By default ,scala Use immutable Set aggregate , If you want to use variable Set aggregate , We need to introduce scala.collection.mutable.Set package .

Definition Set The syntax format of the set is as follows :

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

scala Provides a lot of operations Set The way to assemble . Next , List some operations Set Common methods of collection .

Method name Related instructions
head obtain Set The first element of the collection
tail Returns all elements except the first Set aggregate
isEmpty if Set Collection is empty , Then return to ture, Otherwise return to false
take obtain Set Before the assembly n Elements
contains Judge Set Whether the collection contains the specified element

Define a Set aggregate site, Use common methods to set site Carry out relevant operations , The code is as follows :

file SetTest.scala:

object SetTest {
  def main(args: Array[String]): Unit = {
    // Definition set aggregate 
    val site = Set("Itcast","Google","Baidu")
    val nums:Set[Int] = Set()

    println(" The first website is :"+site.head)
    println(" The last website is :"+site.tail)
    println(" View set site Is it empty :"+site.isEmpty)
    println(" View set nums Is it empty :"+nums.isEmpty)
    println(" see site The first two websites of :"+site.take(2))
    println(" Check whether the collection contains websites Itcast:"+site.contains("Itcast"))
  }
}

3.3 Map

stay scala in ,Map Is an iterative key value pair (key/value) structure , And the key is unique . If you need to use variable Map aggregate , We need to introduce import scala.collection.mutable.Map class .

Definition Map The syntax format of the set is as follows :

var A:Map[Char,Int] = Map( key  ->  value , key  -> value ...)  //Map Key value pair , The key is Char, The value is Int

scala It also provides many operations Map The way to assemble , Next , List some operations Map Common methods of collection :

Method name Related instructions
() Find the corresponding value according to a key , Be similar to java Medium get()
contains() Check Map Contains a specified key in
getOrElse() Determine whether the key is included , If it contains, return the corresponding value , Otherwise, return to other
keys return Map All the keys (key)
values return Map All the values (value)
isEmpty if Map It's empty time , return ture

Define a Map aggregate colors, Use Map Common methods are set colors Close first , The code is as follows :

file MapTest.scala:

object MapTest {
  def main(args: Array[String]): Unit = {
    // Definition Map aggregate 
    val colors = Map("red" -> "#FF0000", "azure" -> "#F0FFFF", "peru" -> "#CD853F")

    val peruColo = if (colors.contains("peru")) colors("peru") else 0
    val azureColo = colors.getOrElse("azure", 0)

    print(" obtain colors The middle key is red Value :" + colors("red"))
    println(" obtain colors All the keys in :" + colors.keys)
    println(" obtain colors All the values of :" + colors.values)
    println(" testing colors Is it empty :" + colors.isEmpty)

    println(" Judge colors Is there a key peru, Including, return the corresponding value , Otherwise return to 0:" + peruColo)
    println(" Judge colors Is there a key azure, Including, return the corresponding value , Otherwise return to 0:" + azureColo)
  }
}

scala Object oriented features

scala It's an object-oriented language

1. Classes and objects

Whether in the scala China or java in , Classes are abstractions of objects , Both objects are concrete instances of classes :

The syntax format of creating a class is as follows :

class  Class name [ parameter list ]

After the class is created , If you want to access the methods and fields in the class , You need to create an object .

The syntax format for creating objects is as follows :

 Class name   Object name  = new  Class name ();

Create a Point class , And define two fields in the class x and y And one with no return value move Method , Use Point Class to access methods and fields in the class , The code is as follows :

file ClassTest.scala:

// Defining classes 
class Point(xc:Int,yc:Int){
  var x:Int = xc
  var y:Int = yc

  def move(dx:Int, dy:Int): Unit = {
    x = x + dx
    y = y + dy

    println("x The coordinate point of :"+ x)
    println("y The coordinate point of :"+ y)

  }
}
object ClassTest {
  def main(args: Array[String]): Unit = {
    // Define class objects 
    val pt = new Point(10, 20)

    // Move to a new location 
    pt.move(10,10)
  }
}

2. Inherit

scala and java similar , Only one parent class is allowed to inherit . The difference is ,java Only non private properties and methods in the parent class can be inherited . and scala You can inherit all properties and methods in the parent class .

stay scala When a subclass inherits a parent class , There are several points to pay attention to :

  • If a subclass wants to override a non abstract method in a parent class , be You have to use override keyword , Otherwise, there will be grammatical errors .

  • If Zi lie wants to override the abstract method in the parent class , be No need to use override keyword .

Create a Pt And a class Location class , also Location Class inheritance Pt class , Demo subclass Location Override parent class Pt In the field , The code is as follows :

file ExtendsTest.scala:

// Define parent class Point class 
class Pt(val xc:Int, val yc:Int){
  var x:Int = xc
  var y:Int = yc

  def move(dx:Int, dy:Int): Unit = {
    x = x + dx
    y = y + dy

    println("x The coordinate point of :" + x)
    println("y The coordinate point of :" + y)
  }
}
// Defining subclasses :Location, Inherit Point class 
class Location(override val xc:Int, override val yc:Int, val zc:Int) extends Pt(xc, yc) {
  var z:Int = zc

  def move(dx:Int, dy:Int, dz:Int):Unit = {
    x = x + dx
    y = y + dy
    z = z + dz

    println("x The coordinate point of :" + x)
    println("y The coordinate point of :" + y)
    println("z The coordinate point of :" + z)
  }
}
object ExtendsTest {
  def main(args: Array[String]): Unit = {
    // Create a subclass object :Location
    val loc = new Location(10, 20, 15)

    // Move to a new location 
    loc.move(10,10,5)
  }
}

3. Singleton and companion objects

stay scala in , There are no static methods or static fields , Therefore, you cannot directly access methods and fields in a class with a class name , Instead, create an instance object of the class to access methods and fields in the class . however ,scala Provided in object This keyword is used to implement the singleton pattern , Use keywords object The object created is Singleton object .

The syntax format of creating a singleton object is as follows :

object objextName

Create a singleton object SingletonObject, The code is as follows :

file Singleton.scala:

// Create singleton objects 
object SingletonObject{
  def hello(): Unit = {
    println("Hello,This is Singleton Object")
  }
}

object Singleton {
  def main(args: Array[String]): Unit = {
    SingletonObject.hello()
  }
}

stay scala in , There is a class and a singleton object in a source file , If the singleton object name is the same as the class name , Then this singleton object is called the companion object (companion object); This class is called the companion class of the singleton object (companion class). Private methods and fields can be accessed between classes and halfling objects .

Define a companion object Dog, Demonstrate private methods and fields in the operation class . The code is as follows :

file Dog.scala:

// Create a class 
// Companion class 
class Dog{
  val id = 666
  private var name = " Two ha "

  def printName(): Unit ={
    // stay Dog Class can access accompanying objects Dog Private fields for 
    println(Dog.CONSTANT + name)
  }
}
// Companion 
object Dog {
  // Add private fields to accompanying objects 
  private var CONSTANT = " Wang Wang Wang ..."
  def main(args: Array[String]): Unit = {
    val dog = new Dog

    dog.name  = " Two ha  666"

    dog.printName()
  }
}

4. Trait

stay scala in ,Trait( Trait ) The function of is similar to java The interface ,scala Medium Trait Can be used by classes and objects (Objects) Use keywords extends To inherit .

The syntax format for creating traits is as follows :

trait traitName

Create a trait Anima, Demonstrate that the class inherits the trait and accesses the operation of the method in the trait . The code is as follows :

file People.scala:

// Defining traits 
trait Animal{
  // Define an abstract method ( There is no way to implement it )
  def speak()
  def listen(): Unit ={

  }
  def run(): Unit ={
    println("I am running")
  }
}

// Defining classes , Inherit character 
class People extends Animal{
  override def speak(): Unit = {
    println("I am speaking English")
  }
}
object People {
  def main(args: Array[String]): Unit = {
    val people = new People

    people.speak()
    people.listen()
    people.run()
  }
}

scala Pattern matching with sample classes

scala Provides a powerful pattern matching mechanism , The most common pattern matching is match sentence , It mainly applies to the scenario of selecting from multiple branches .

1. Pattern matching

scala The pattern matching in is by match case Composed of , It is similar to java Medium switch case, That is, conditional judgment on a value , For different conditions , Different treatment .

 expression  match {
    case  Pattern 1 =>  sentence 1
    case  Pattern 2 =>  sentence 2
    case  Pattern 3 =>  sentence 3
}

Define a method matchTest(), The parameter of the method is an integer field , The method call is to pattern match the parameters , If the parameter matches 1, Then print out "one"; If the parameter matches 2. Then print out ”two“, If the parameter matches _, Then print out "many", The code is as follows :

file PatternMatch.scala:

object PatternMatch {
  def main(args: Array[String]): Unit = {
    println(matchTest(1))
  }
    def matchTest(x:Int):String = x match {
        case 1 => "one"
        case 2 => "two"
        case _ => "many"
      }
}

2. Sample class

stay scala in , Use case The class defined by keyword is called sample class . The sample class is a special class , After optimization, it can be used for pattern matching . We use case Define the sample class Person, And apply the sample class to pattern matching , The code is as follows :

file CaseClass.scala:

object CaseClass {
  // Define the sample class 
  case class Person(name:String, age:Int)
  def main(args: Array[String]): Unit = {
    // Create sample class objects 
    val alice = new Person("Alice", 25)
    val bob = new Person("Bob", 32)
    val charlie = new Person("charlie", 32)
//    val tom = Person("tom", 25)

    // Use sample classes for pattern matching 
    for (person <- List(alice, bob, charlie)){
      person match {
        case Person("Alice", 25) => println("Hi Alice")
        case Person("Bob", 32) => println("Hi Bob")
        case Person(name, age) => println("Name:" + name + "\t" + "age:" + age)
      }
    }
  }
}
原网站

版权声明
本文为[LackyQueen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061128575058.html