Small note -------- scala-pattern matching

Pattern matching, similar to the switch case syntax in java, that is, conditional judgment on a value, and then for a certain condition, for different processing
    But the function of scala's pattern matching is much more powerful than that of java's switch case syntax. Java's switch case syntax can only match values, but in addition to matching values, scala's pattern matching can also type Matching, matching the elements of Array and List, matching the case class (sample class), and even matching the valued or no value (Option).
    And for spark, the pattern matching function of scala is also extremely important. The pattern matching function is used a lot in the spark source code. Therefore, in order to write the scala program better and understand the spark source code more smoothly.
 
Basic Grammar of Pattern Matching (Case: Performance Evaluation)
    Scala provides a more powerful match case syntax, namely pattern matching. It can match various types: such as the type of variable, the elements of the collection, with or without value
    Syntax of match case: variable match {case value => syntax}
    If the value is underlined, it means how to deal with the default situation in all cases above.
    In the match case, the main case branch is satisfied and processed, and it will not continue to judge the next case branch.
 
Pattern matching on types (case: exception handling)
    Scala's pattern matching can directly match types, not values
    Syntax: variable match case variable: type => code
 
Pattern matching of elements of Array and List (Case: Say hello to friends)
    Perform pattern matching on Array to match arrays with specified elements, arrays with specified number of elements, and arrays beginning with a certain element
   
Pattern matching on List, similar to Array, but need to use List specific :: operator
case class and pattern matching (case: school access control)
     A special class is provided in scala. Case class declaration is also called a sample class.
    Case class is a bit similar to the concept of javaBean in java, only defines the field, and automatically provides Getter and setter methods when compiled by scala, but there is no method that is a method
    The parameters received by the main constructor of the case class usually do not need to be decorated with var or val. Scala will automatically use val decoration unless you define var yourself. Otherwise, the default is val
    Scala automatically defines companion objects for the case class, that is, objects of the same name. And defines the apply () method, the method receives the same parameters in the main constructor, and returns the case class object
 
Option and pattern matching (case: query results)
    scala has a special type called Option,
    Option has two values, one is that Some has a value,
                                One is None means there is no value
    Option is usually used in pattern matching to determine whether a variable has a value or no value, which is more concise than null
    The usage of Option must be mastered. Spark source code uses a lot of Options such as Some (a) None etc.
Change the incoming parameter to Int type
    First invert the key and value of the map, and then pass the gg function into the Int type to determine the Int type
 
 

Guess you like

Origin www.cnblogs.com/yzqyxq/p/12683386.html