Scala notes finishing (6): Scala collection library

[TOC]


Scala main collection structure

1. The collection system in Scala mainly includes: Iterable, Seq(IndexSeq), Set(SortedSet), Map(SortedMap). where Iterable is the root trait of all collection traits. In fact, Seq, Set, and Map are all sub-traits

  • Seq

    is an ordered sequence of values, such as an array or list. IndexSeq allows us to quickly access any element through the table below. For example, ArrayBuffers are subscripted, but linked lists are not.

  • Set

    is an unordered set of values. In a SortedSet, elements are accessed in some sort order.

  • Map

    is a set of (key, value) pairs. SortedMap accesses the entities in it in order of keys.

2. Collections in Scala are divided into two types: mutable and immutable collections. A mutable collection means that the elements of the collection can be dynamically modified, while the elements of an immutable collection cannot be modified after initialization. Corresponding scala.collection.mutableand scala.collection.immutabletwo packages respectively.

3. Seq contains sub-traits such as Range, , ArrayBuffer, Listetc. Among them, Range represents a sequence, and you can usually use the syntax "1 to10" to generate a Range. ArrayBuffer is similar to ArrayList in Java.

List

List common operations 1

1. In Scala, a list is either Nil (and an empty list), or a head element plus a tail, which in turn is a list

scala> val list = List(1, 2, 3, 4, 5)
list: List[Int] = List(1, 2, 3, 4, 5)

scala> list.head
res29: Int = 1

scala> list.tail
res30: List[Int] = List(2, 3, 4, 5)

scala> list.isEmpty
res31: Boolean = false

scala> list == Nil
res32: Boolean = false

Here is an example of using recursion to find the neutralization of a list:

def recursion(list:List[Int]):Int = {
    if(list.isEmpty) {
        return 0  // 使用return显式结束程序的运行,否则0只是该if语句的返回值,并不会结束程序的运行,当然如果用else不用return也行
    }
    list.head + recursion(list.tail)
}

List common operations 2

Increase

//增
/*  A.++(B)  --> 在列表A的尾部对添加另外一个列表B,组成一个新的列表
 *  A.++:(B) --> 在列表A的首部对添加另外一个列表B,组成一个新的列表
 *  A.:::(B) --> 在列表A的首部对添加另外一个列表B,组成一个新的列表
 *  ------
 *  A.:+ (element) -->在列表A的尾部添加一个element,组成一个新的集合
 *  A.+: (element) -->在列表A的首部添加一个element,组成一个新的集合
 *  A.:: (element) -->在列表A的首部添加一个element,组成一个新的集合
 */

The test is as follows:

scala> val left = List(1, 2, 3, 4)
left: List[Int] = List(1, 2, 3, 4)

scala> val right = List(5, 6, 7)
right: List[Int] = List(5, 6, 7)

scala> left.++(right)
res33: List[Int] = List(1, 2, 3, 4, 5, 6, 7)

scala> left.++:(right)
res34: List[Int] = List(5, 6, 7, 1, 2, 3, 4)

scala> left.:::(right)
res35: List[Int] = List(5, 6, 7, 1, 2, 3, 4)

scala> left.:+(10)
res36: List[Int] = List(1, 2, 3, 4, 10)

scala> left.+:(10)
res38: List[Int] = List(10, 1, 2, 3, 4)

scala> left.::(10)
res39: List[Int] = List(10, 1, 2, 3, 4)

delete

drop(n)         --->删除list的前n个元素(首部开始删除)
dropRight(n)    --->删除list的后n个元素(尾部开始删除)
dropWhile(p: A => Boolean)  --->逐个匹配去除符合条件的元素,直到不符合条件,之后的元素不再判断

The test is as follows:

scala> val list = List(1, 2, 3, 4, 5, 6, 7)
list: List[Int] = List(1, 2, 3, 4, 5, 6, 7)

scala> list.drop(2)
res52: List[Int] = List(3, 4, 5, 6, 7)

scala> list.dropRight(3)
res53: List[Int] = List(1, 2, 3, 4)

scala> list.dropWhile(_ <= 3)
res54: List[Int] = List(4, 5, 6, 7)

scala> list.dropWhile(_ > 3)    // 第一个元素就不符合条件,后面的不再判断,所以一个也没有删除
res55: List[Int] = List(1, 2, 3, 4, 5, 6, 7)

Modify and query

scala> val list = List(1, 2, 3, 4, 5, 6, 7)
list: List[Int] = List(1, 2, 3, 4, 5, 6, 7)

scala> list(0)
res57: Int = 1

scala> list(0) = 10
<console>:9: error: value update is not a member of List[Int]
              list(0) = 10
              ^

The default List is in:

scala> List
res59: scala.collection.immutable.List.type = scala.collection.immutable.List$@3e98fd35

So it cannot be modified, but trying to import a modifiable List also fails to import:

scala> import scala.collection.mutable.List
<console>:8: error: object List is not a member of package scala.collection.mutable
       import scala.collection.mutable.List

The reasons are as follows:

Scala 列表类似于数组,它们所有元素的类型都相同,但是它们也有所不同:
列表是不可变的,值一旦被定义了就不能改变,其次列表 具有递归的结构(也就是链接表结构)而数组不是。

List common operations 3

scala> val list = List(1, 2, 3, 4, 5, 6, 7)
list: List[Int] = List(1, 2, 3, 4, 5, 6, 7)

scala> list.take(5)
res60: List[Int] = List(1, 2, 3, 4, 5)

scala> list.takeWhile(_ <= 3)
res61: List[Int] = List(1, 2, 3)

scala> list.takeWhile(_ > 3)
res62: List[Int] = List()

scala> list.mkString
res63: String = 1234567

scala> list.count(_ % 2 == 0)
res64: Int = 3

scala> val fruit = "apples" :: ("oranges" :: ("pears" :: Nil))  // ::操作符从给定的头和尾部创建一个新的列表
fruit: List[String] = List(apples, oranges, pears)

scala> val list1 = List(1, 2, 3)
list1: List[Int] = List(1, 2, 3)

scala> val list2 = List(4, 5, 6)
list2: List[Int] = List(4, 5, 6)

scala> list1 ++ list2   // ++两个集合之间的操作
res70: List[Int] = List(1, 2, 3, 4, 5, 6)

scala> list1.sum
res72: Int = 6

Set

1. A dataset is a collection of non-repeating elements. Attempting to add an existing element to it has no effect. for example

def setOps:Unit ={
    (Set(2,0,1) + 1).foreach(println(_))
}

2. Set does not preserve the order in which elements are inserted. By default, sets are implemented as hashsets whose elements are organized according to the value of the hashCode method.

Set(1,2,3,5,7,8).foreach(println(_))

3. Chained hash sets can remember the order in which elements were inserted. It maintains a linked list for this purpose.

val weeks= scala.collection.mutable.LinkedHashSet("Mo","Tu","We","Th","Fr")
weeks.foreach(println(_))

4. Access the elements in the sorted order

scala.collection.immutable.SortedSet(1,2,3,4,5,6).foreach(println(_))

set collection order description

Look directly at the test code and comments below:

package cn.xpleaf.bigdata.p4.collection

import scala.collection.mutable.SortedSet

object _02CollectionOps {
  def main(args: Array[String]): Unit = {
    setOps1
  }

  /**
    * 关于scala集合顺序的说明
    *     测试1:按照年龄进行升序,年龄相等,姓名降序
    *     测试2:要按照姓名升序比较,按照年龄升序比较
    * SortedSet在添加普通对象Person之类,报错
    *   No implicit Ordering defined for Person
    *   要想在有序集合中添加元素,必须要让元素具备比较性,要和给集合提供比较器
    *   在java中前者是要类实现Comparator,后者需要给集合提供一个比较器Comparator
    *   在scala中,前者需要让类扩展Ordered的特质,后者给集合传递一个Ordering比较器
    *
    *   当两个比较都实现的话,优先使用集合的比较器
    */
  def setOps1: Unit = {
    var set = SortedSet[Person]()(new Ordering[Person] {
      override def compare(x: Person, y: Person):Int = {
        var ret = x.getName.compareTo(y.getName)
        if(ret == 0) {
          ret = x.getAge.compareTo((y.getAge))
        }
        ret
      }
    })
    set.+= (new Person("王立鹏", 19))
    set.+= (new Person("冯 剑", 18))
    set.+= (new Person("刘银鹏", 15))
    set.+= (new Person("李小小", 19))
    // println(set)
    set.foreach(println(_))
  }
}

class Person extends Ordered[Person] {
  private var name:String = _

  private var age:Int = _

  def this(name:String, age:Int) {
    this()
    this.name = name
    this.age = age
  }

  def getAge = age

  def getName = name

  override def toString: String = {
    s"[$name, $age]"
  }

  /**
    * 按照年龄进行升序,年龄相等,姓名降序
    * 升序:前面比后面
    * 降序:后面比前面
    */
  override def compare(that: Person) = {
    var ret = this.age.compareTo(that.age)
    if(ret == 0) {
      ret = that.name.compareTo(this.name)
    }
    ret
  }
}

The output is as follows:

[冯 剑, 18]
[刘银鹏, 15]
[李小小, 19]
[王立鹏, 19]

Functional programming for collections

1. The functional programming of collections is very important. This is the programming method we will use every day in our future work.

2. You must fully grasp and understand the meaning of Scala's higher-order functions. The functions such as map, flatMap, reduce, reduceLeft, and foreach of Scala's collection classes are higher-order functions, because they can receive other functions as parameters (part of higher-order functions). Functions can refer to the functional programming notes organized earlier)

3. The use of higher-order functions is also the biggest difference between Scala and Java! ! ! Because Java did not have functional programming before 1.8, and certainly did not have higher-order functions, and certainly cannot directly pass a function into a method, or let a method return a function

4. The understanding, mastery and use of Scala higher-order functions can greatly enhance your technology, and it is also one of the most attractive and advantageous functions of Scala.

5. In the Spark source code, there are a lot of functional programming, so you must master it in order to understand the spark source code.

  • The foreach method can apply a function to each element in a collection and produce a collection of results
scala> val names = List("Peter","Paul","Mary")
names: List[String] = List(Peter, Paul, Mary)

scala>  names.map(_.toUpperCase).foreach(println(_))
PETER
PAUL
MARY
  • Each Map function is followed by a =="
scala> List("Peter","Paul","Mary").map(_ + "===>name").foreach(println(_))
Peter===>name
Paul===>name
Mary===>name
  • Flatmap is divided by spaces
scala> List("Peter Glad","Paul Hello","Mary Your").flatMap(_.split(" ")).foreach(println)
Peter
Glad
Paul
Hello
Mary
Your

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324654443&siteId=291194637