1 foreach

Foreach main format

foreach(f: (A) = >Unit) :UnitParameter f: (A) ⇒ UnitReceives a function object <br /> whose input arguments are elements of the collection and whose return value is nullUnitCopy the code

The sample

// Define a list
scala> val a = List(1.2.3.4)
a: List[Int] = List(1.2.3.4)

// Print iteratively
scala> a.foreach((x:Int)=>println(x))
scala> a.foreach(x=>println(x))
scala> a.foreach(println(_))
Copy the code

2 map

format

def map[B](f: (A) ⇒ B) :TraversableOnce[B] | | generic type [B] | specified map method eventually return to a collection of generic | | | parameters f: (A) ⇒ B| pass in a function object < br / > the function receives a typeA(the list element to be converted), returns a typeB| | | return valuesTraversableOnce[B] | BThe set of type |Copy the code

The sample

The process of converting one data type to another. scala> a.map(x=>x+1)
res4: List[Int] = List(2.3.4.5)
/ / short
scala> a.map(_ + 1)
Copy the code

3 flatMap

format

// flatMap is a map and then a flatten
def flatMap[B](f: (A) ⇒ GenTraversableOnce[B) :TraversableOnce[B] | | generic type [B] | eventually want to convert a collection of element type | | | parameters f: (A) ⇒ GenTraversableOnce[B] | pass in a function object < br / > function parameter is a collection of elements of < br / > function return value is a collection of | | | return valuesTraversableOnce[B]             | BThe set of type |Copy the code

The sample

1, first perform map operation on its list, and then flatten// Define a text row list
scala> val a = List("hadoop hive spark flink flume"."kudu hbase sqoop storm")
a: List[String] = List(hadoop hive spark flink flume, kudu hbase sqoop storm)

// Use map to convert lines of text into arrays of words
scala> a.map(x=>x.split(""))
res5: List[Array[String]] = List(Array(hadoop, hive, spark, flink, flume), Array(kudu, hbase, sqoop, storm))

// Flatten the array
scala> a.map(x=>x.split("")).flatten
res6: List[String] = List(hadoop, hive, spark, flink, flume, kudu, hbase, sqoop, storm)

2Scala > a.latmap (_.split(""))
res7: List[String] = List(hadoop, hive, spark, flink, flume, kudu, hbase, sqoop, storm)
Copy the code

4 filter

format

def filter(p: (A) ⇒ Boolean) :TraversableOnce[A| | p parameters: (A) ⇒ Boolean| pass in a function object < br / > to receive a collection type parameter < br / > return Boolean type, satisfy conditions returntrue, does not satisfy the returnfalse| | | return valuesTraversableOnce[AList] | |Copy the code
// The result returned is the result that satisfies the condition
scala> List(1.2.3.4.5.6.7.8.9).filter(_ % 2= =0)
res8: List[Int] = List(2.4.6.8)
Copy the code

5 Sorted, sortBy, sortWith

5.1 sorted

Reference code

scala> List(3.1.2.9.7).sorted
res16: List[Int] = List(1.2.3.7.9)
Copy the code

5.2 sortBy


def sortBy[B](f: (A) ⇒ B) :List[A] | | generic type [B] | sorted according to what type | | | parameters f: (A) ⇒ B| incoming function object < br / > receives a collection type < br / > element parameters of the returnBTypes of element sorted | | | return valuesList[A] return to sorted list | |Copy the code

Reference code

scala> val a = List("01 hadoop"."02 flume"."03 hive"."04 spark")
a: List[String] = List(01 hadoop, 02 flume, 03 hive, 04 spark)

// Get the word field
scala> a.sortBy(x => x.split("") (1))
/ / short
scala> a.sortBy(_.split("") (1))
res8: List[String] = List(02 flume, 01 hadoop, 03 hive, 04 spark)
Copy the code

5.3 sortWith

format

def sortWith(lt: (A.A) ⇒ Boolean) :List[A] | | parameters lt: (A.A) ⇒ Boolean| to the size of a comparison function objects < br / > receive two element parameters of the collection type < br / > returns two element size, less than returntrue, greater than returnfalse| | | return valuesList[A] return to sorted list | |Copy the code

Reference code

scala> val a = List(2.3.1.6.4.5)
a: List[Int] = List(2.3.1.6.4.5)

scala> a.sortWith((x,y) => if(x<y)true else false)
res15: List[Int] = List(1.2.3.4.5.6)

scala> res15.reverse
res18: List[Int] = List(6.5.4.3.2.1)

Function arguments appear only once in a function and can be replaced with underscores
scala> a.sortWith(_ < _).reverse
res19: List[Int] = List(6.5.4.3.2.1)
Copy the code

6 groupBy

format

def groupBy[K](f: (A) ⇒ K) :Map[K.List[A]] | generic | [K] | grouping field type | | | parameters f: (A) ⇒ K| pass in a function object < br / > to receive the collection element type parameter returns < br / > aKType key, this key will be used for grouping, the same key in a set of | | | return valuesMap[K.List[A]] | returns a map,KIs the grouping field,ListA set of data for the grouping field corresponding |Copy the code

Reference code

scala> val a = List("Zhang"->"Male"."Bill"->"Female"."Fifty"->"Male")
a: List[(String.String)] = List(Zhang SAN, male) li Si, female) Wang Wu, male)// Group by gender
scala> a.groupBy(_._2)
res0: scala.collection.immutable.Map[String.List[(String.String=)]]Map(male - >List(Zhang SAN, male), (Wang Wu, male), female ->List(Li Si, female))// Convert the grouped mapping to a list of gender/number tuples
scala> res0.map(x => x._1 -> x._2.size)
res3: scala.collection.immutable.Map[String.Int] = Map(male - >2Female - >1)
Copy the code

7 reduce

7.1 the reduce

format

def reduce[A1> :A](op: (A1.A1) ⇒ A1) :A1| | generic type [A1> :A] | (lower bound)A1The element type must be set the parent of the | | | parameters op: (A1.A1) ⇒ A1| incoming function object, used to ongoing aggregation operations < br / > the firstA1The type parameters are: currently aggregated variable <br /> secondA1Type parameters as follows: the current to the element of aggregated | | | return valuesA1| | list eventually aggregated into one elementCopy the code

Reference code

scala> val a = List(1.2.3.4.5.6.7.8.9.10)
a: List[Int] = List(1.2.3.4.5.6.7.8.9.10)

scala> a.reduce((x,y) => x + y)
res5: Int = 55

// The first underscore represents the first parameter, which is the historical aggregate data result
// The second underscore represents the second parameter, which is the current data element to aggregate
scala> a.reduce(_ + _)
res53: Int = 55

// As with reduce, it is calculated from left to right
scala> a.reduceLeft(_ + _)
res0: Int = 55

// Aggregate from right to left
scala> a.reduceRight(_ + _)
res1: Int = 55
Copy the code

7.2 fold

format

def fold[A1> :A](z: A1)(op: (A1.A1) ⇒ A1) :A1| | generic type [A1> :A] | (lower bound)A1The element type must be set the parent of the | | parameters1      | z: A1Initial value | | | parameters2      | op: (A1.A1) ⇒ A1| incoming function object, which is used to continuously for folding operation < br / > the firstA1The type parameters are: currently folded variable <br /> secondA1Type parameters as follows: the element to be folded | | | return valuesA1| | list finally fold for a elementsCopy the code

Reference code

scala> val a = List(1.2.3.4.5.6.7.8.9.10)
a: List[Int] = List(1.2.3.4.5.6.7.8.9.10)

scala> a.fold(0)(_ + _)
res4: Int = 155
Copy the code