What are the symbols in scala
This article introduces the relevant knowledge of "what are the symbols in scala". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Symbols in Scala:
: three colon operators: indicates the concatenation operation of list
Val one = List (1 and 2) val two = List (4 and 5) val three = one:::two
The output is three: List [Int] = List (1, 2, 3, 4, 5, 6)
:: two colon operators: indicates the join operation between a normal element and a list
The _ N underscore numeric operator: used to access the Nth element of the tuple, the value of N starts at 1. The element access method of a tuple is different from an array because the element type of a tuple can be different.
-> returns a binary tuple
Return: (List [Int], List [Int]) = (List (1,2,3), List (4,5,6))
Change what is on the left to something on the right; it can be seen as a syntax candy for creating an instance of a function. There are three common operators:
= >: Example:Int = > String indicates that the input of the function is int type and string type is returned, which is the same as Function (Int,String)
() = >: Example: () = > T indicates that the function has no input parameters, but returns T
Unit= >: Unit is equivalent to no value, equivalent to Void in C++
_ has many meanings
1. Wildcard characters
Import org.apache.spark.SparkContext._
2. Anonymous function
3. Refer to each element in the collection. For example, filter elements in a list that are greater than a certain value
Val lst = List (1, 2, 3, 4, 5) val lstFilter = lst.filter (_ > 3)
4. Pattern matching can be used to get the team members of a tuple.
Val m = Map (1-> 2 for 2-> 4)