Lesson 32: the basic operation of List and the implementation of List sorting algorithm based on pattern matching
package com.dt.scala.datasetobject HelloList { def main(args: Array[String]): Unit = { //define List, directly use applay method val bigData = List("Spark","Hadoop") val data = List(1,2,3) //use:: definition list val bigData_core = "Spark"::"Hadoop"::Nil //:: Nil cannot be omitted,"Spark"::"Hadoop" syntax is wrong //Nil is an empty list and:: is a method of the right element, e.g."Hadoop"::Nil :: is a method of Nil //equivalent to val bigData_core2 = Nil.:: ("Hadoop") val data_Int = 1::2::3::Nil //merge two lists using::: method val data_union = data ::: data_Int //Determine if the list is empty data.isEmpty //get the head of the list data.head //For List, the first element is called head, and all other elements are called tail data.tail.head //List elements in advance val List(a,b) = bigData //a="Spark" , b="Hadoop" println("a = "+a+" b = "+b) //If there are multiple elements in the List, you can extract them as follows val first::second::rest = data //rest represents the rest of the List, the type returned is List println("first:"+first+" ==== "+"second:"+second+" ==== "+"rest:"+rest) //first:1 ==== second:2 ==== rest:List(3) //Sort List using pattern matching val shuffleData = List(9,3,2,10,3,34,1) def compute(data : Int,dataSet : List[Int]) : List[Int] = dataSet match { case List() => List(data) //if dataSet is empty, return List(data) case head :: tail => { //if dataSet is not empty if (data compute(head,sortList(tail)) } println(sortList(shuffleData)) }}