scala -- set explanation, set related method introduction, Traversable use

1. Assembly

1.1 general

Anyone who has learned about programming knows that the sentence "program = algorithm + data structure" was put forward by the famous Swiss computer scientist Nicholas Voss, who was also the winner of the Turing Award in 1984. Algorithm refers to a series of effective and general steps of calculation. Algorithm and data structure are two complementary aspects in program design, Therefore, data structure is also a very important aspect of programming. Many programming languages provide the corresponding programming library of data structure,

It is also called collection library. There are also collection libraries in scala. Its advantages are as follows:

  • Easy to use

    Most set problems can be solved by using about 20 to 50 methods provided by set library and flexible combination

  • concise

    A simple word (foreach, for example) can implement one or more loop operations

  • security

    Most errors can be found at compile time

  • fast

    The methods of collection type are optimized, and users can select the appropriate collection according to their needs

  • unified

    Scala collections have a very rigorous inheritance system. Collections of similar types have the same set of methods and their own unique methods

1.2 classification

Scala supports both immutable sets and variable sets. Because immutable sets can be accessed safely and concurrently, it is also the default collection class library. In Scala, almost all collection classes are provided with variable and immutable versions, as follows:

  • Immutable set: the elements in the set cannot be changed once initialization is completed. Any change to the set will generate a new set

    They are all under the package scala.collection.immutable, and there is no need to manually import the package

  • Variable set: it means that the set itself can change dynamically, and the variable set provides a method to change the elements in the set

    They are all under the package scala.collection.mutable. Manual package import is required

As shown below:

Tips:

  1. Variable sets are richer than immutable sets

    For example, the Buffer set is added to the Seq set. We commonly use ArrayBuffer and ListBuffer

  2. When we come into contact with a new inheritance system, it is recommended to adopt the top-level learning and the bottom-level way

    • The top level defines the content common to the entire inheritance system

    • The bottom layer is the concrete embodiment and implementation

2. Traversable

2.1 general

Traversable is a trait, which is the parent trait of other sets. Its child traits immutable.Traversable and mutable.Traversable are the parent traits of immutable sets and mutable sets respectively. Most common methods in a set are defined in this trait. Therefore, understanding its function is very important to learn other set classes

2.2 format

  • Format 1: create an empty Traversable object

    //Method 1: implemented by empty method
    val t1 = Traversable.empty[Int]
    
    //Method 2: implemented by parentheses
    val t2 = Traversable[Int]()
    
    //Method 3: realized through Nil
    val t3 = Nil
    
  • Format 2: create Traversable object with parameters

    //Method 1: implemented by toTraversable() method
    val t1 = List(1, 2, 3).toTraversable
    
    //Method 2: it is implemented through the apply() method of the associated object of Traversable 
    val t1 = Traversable(1, 2, 3)
    

2.3 example 1: creating Traversable objects

  1. Create an empty Traversable object to store Int type data
  2. Create a Traversable collection object, store the numbers 1, 2, 3, and print the results to the console

Reference code

//Case: demonstrates creating a Traversable object
object ClassDemo01 {
  def main(args: Array[String]): Unit = {
    //1. Create an empty Traversable object to store Int type data
    //1.1 create objects
    val t1: Traversable[Int] = Traversable.empty[Int]
    val t2: Traversable[Int] = Traversable[Int]()
    val t3: Traversable[Int] = Nil
    //1.2 compare them for equality
    println(t1 == t2)   //==Compare the data in the set
    println(t1 == t3)
    println(t2 == t3)

    println(t1 eq t2)   //eq compares the address values of a collection
    println(t1 eq t3)
    println(t2 eq t3)
    println("-" * 15)

    //2. Create a Traversable collection object, store the numbers 1, 2 and 3, and print the results to the console
    //2.1 it is implemented by toTraversable() method
    val t4: Traversable[Int] = List(1, 2, 3).toTraversable
    val t5: Traversable[Int] = Set(1, 2, 3).toTraversable

    //2. It is implemented through the apply() method of Traversable's companion object
    val t6:Traversable[Int] = Traversable(11, 22, 33, 44, 55)

    //3. Print results (because Traversable is a trait, the bottom layer is still implemented through its specific subclasses)
    println(s"t4: ${t4}")
    println(s"t5: ${t5}")
    println(s"t6: ${t6}")
  }
}

2.4 case 2: transpose Traversable set

Students who have learned about linear algebra know that a matrix has a transpose operation. In Scala, a similar operation can be realized through the transpose() method

As shown below:

be careful:

When transposing, the program will automatically detect whether the number of elements in each set is consistent. If consistent, the transpose is successful. If inconsistent, an error is reported

demand

  1. Define a Traversable set t1, which has three elements. Each element is a Traversable set and stores the following data respectively:
  2. The first element stores (1, 4, 7), the second element stores (2, 5, 8), and the third element stores (3, 6, 9)
  3. transpose the set t1 through the transfer method
  4. Print results

Reference code

//Case: demonstrate transpose set
object ClassDemo02 {
  def main(args: Array[String]): Unit = {
    //1. Define a Traversable set t1, which has three elements. Each element is a Traversable set and stores the following data respectively:
    //2. The first element stores (1, 4, 7), the second element stores (2, 5, 8), and the third element stores (3, 6, 9)
    val t1 = Traversable(Traversable(1, 4, 7), Traversable(2, 5, 8), Traversable(3, 6, 9))
    //3. transpose the set t1 through the transfer method
    val t2 = t1.transpose
    //4. Print results
    println(t2)
  }
}

2.5 case 3: splicing collection

In the actual development, data is obtained from multiple channels, so we often need to splice some data. In Scala, we can splice data through + +, but this method will create a large number of temporary sets (that is, every time + +, a new temporary set will be created). In this case, we can use concat() Method. This method will calculate the size of the required set in advance, and then generate a set, which reduces the useless temporary set in the middle, so it is more effective

demand

  1. It is known that there are three Traversable sets, which store (11, 22, 33), (44, 55), (66, 77, 88, 99) elements respectively
  2. The above three sets are spliced by concat() method
  3. Print the spliced results to the console

Reference code

//Case: demonstrate concat() method and splice collection
object ClassDemo03 {
  def main(args: Array[String]): Unit = {
    //1. It is known that there are three Traversable sets, which store (11, 22, 33), (44, 55), (66, 77, 88, 99) elements respectively
    val t1 = Traversable(11, 22, 33)
    val t2 = Traversable(44, 55)
    val t3 = Traversable(66, 77, 88, 99)
    //2. Splice the above three sets through concat() method
    val t4 = Traversable.concat(t1, t2, t3)
    //3. Print the spliced results to the console
    println(s"The result after splicing is: ${t4}")
  }
}

2.6 case 4: screening elements by partial function

In Scala, we can also use the collect() method to implement partial functions in combination with collections, so as to filter the specified data from collections

format

def collect[B](pf: PartialFunction[A, B]): Traversable[B]

Explanation:

  1. [B] Represents the data type of the returned value after partial function processing
  2. pf: PartialFunction[A, B] indicates that the collect() method needs to pass in a partial function object
  3. Traversable[B] represents a collection of specific data returned

demand

  1. It is known that there is a Traversable set, and the storage elements are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  2. Filter out all even numbers in the collection through the collect method

Reference code

//Case: filter out all even numbers in the set through partial function
object ClassDemo04 {
  def main(args: Array[String]): Unit = {
    //1. It is known that there is a Traversable set, and the storage elements are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
    val t1 = (1 to 10).toTraversable

    //2. Filter out all even numbers in the collection through the collect method
    val t2 = t1.collect( {
      case x if x % 2 == 0 => x
    })

    //3. Print results
    println(t2)
  }
}

2.7 case 5: calculate the factorial of set elements

Suppose a Traversable[Int] set contains five numbers (1, 2, 3, 4, 5). If we calculate the factorial of each element and put it into a new Traversable[Int] set, we can implement it by recursion, but this implementation has disadvantages. Each calculation is calculated from scratch. For example, obtaining the factorial of 5 is calculated by 5 * 4 * 3 * 2 * 1, The factorial result of 4 calculated before is not used. At this time, we can optimize this requirement through the scan() method. It not only puts the intermediate calculation result into a new set, but also passes the intermediate result to the next function call

format

def scan[B](z: B)(op: (B, B) => B)

Explanation:

  1. [B] Represents the data type of the return value
  2. (z: B) indicates the initialization value
  3. (OP: (B, b) = > b) represents a specific operation function
  4. The scan() method is equivalent to the scanLeft() method, and there is an opposite method, scanRight()

demand

  1. Define the Traversable set t1 to store the five numbers 1, 2, 3, 4 and 5
  2. Assuming that the initial value is 1, the factorial values of each element in t1 set are obtained by scan() method
  3. Print results

Reference code

//Case: obtain the factorial value of the elements in the set through the scan() method
object ClassDemo05 {
  def main(args: Array[String]): Unit = {
    //1. Define Traversable set t1 and store the five numbers 1, 2, 3, 4 and 5
    val t1 = Traversable(1, 2, 3, 4, 5)
    //2. Assuming the initial value is 1, obtain the factorial values of each element in t1 set through scan() method
    val t2 = t1.scan(1)(_ * _)
    //3. Print results
    println(t2)
  }
}

2.8 case 6: get the specified element of the set

The collection is used to store data. Since it can be stored, we can certainly get the data we want from the collection, which can be realized by the following methods:

  • head: get the first element of the collection. If the element does not exist, throw NoSuchElementException

  • Last: get the last element of the collection. If the element does not exist, throw NoSuchElementException

  • headOption: get the first element of the collection, and the return value type is Option

  • lastOption: get the last element of the collection, and the return value type is Option

  • find: finds the first element in the collection that meets the specified criteria

  • slice: intercepts some elements in the collection

    def slice(from:Int, until: Int): Traversable[A]	
    

    be careful:

    Intercept the elements from the start index to the end of the until index, including the from index but not the until index

demand

  1. Define a Traversable set containing six elements: 1, 2, 3, 4, 5 and 6
  2. Get the first and last elements in the set through head, headoption, last and lastoption, and print them
  3. Get the first even number in the set through the find method and print it
  4. Get the three elements 3, 4 and 5 through the slice() method, put them into a new Traversable collection, and then print the results

Reference code

//Case: get specific elements of Traversable objects
object ClassDemo06 {
  def main(args: Array[String]): Unit = {
    //1. Define a Traversable set containing six elements: 1, 2, 3, 4, 5 and 6
    val t1 = (1 to 6).toTraversable
    //2. Obtain the first and last elements in the set through head, headoption, last and lastoption, and print them
    println(t1.head)
    println(t1.last)
    println(t1.headOption)
    println(t1.lastOption)
    //3. Get the first even number in the set through the find method and print it
    println(t1.find(_ % 2 == 0))
    //4. Get the three elements 3, 4 and 5 through slice() method, and then print the results
    val t2 = t1.slice(2, 5)
    println(t2)
  }
}

2.9 case 7: judge whether the element is legal

If we encounter the need to judge whether all elements in the collection meet the specified conditions, or any element meets the specified conditions, we can consider using the forall() method and the exists() method

  • forall(): returns true if all elements in the collection meet the specified conditions; otherwise, returns false

    def forall(p: (A) => Boolean): Boolean
    
  • exist(): returns true as long as any element in the collection meets the specified conditions; otherwise, returns false

    def exists(p: (A) => Boolean): Boolean
    

demand

  1. Define the Traversable set t1, which contains six numbers from 1 to 6
  2. Through the forall() method, judge whether the elements in t1 are even
  3. Through the exists() method, judge whether there is an even number in t1

Reference code

//Case: judge whether the element is legal
object ClassDemo07 {
  def main(args: Array[String]): Unit = {
    //1. Define Traversable set t1, which contains six numbers from 1 to 6
    val t1 = (1 to 6).toTraversable
    //2. Use the forall() method to judge whether the elements in t1 are even
    println(t1.forall(_ % 2 == 0))    //All elements must meet the conditions
    //3. Use the exists() method to judge whether there is an even number in t1
    println(t1.exists(_ % 2 == 0))    //As long as one element satisfies the condition
  }
}

2.10 case 8: aggregate function

If we want to count the number of elements that meet the conditions in the set, or calculate the sum, product, maximum and minimum of set elements, we can use the following methods:

  • count: counts the number of qualified elements in the collection

    def count(p: (A) => Boolean): Int
    
  • sum: get all elements and in the collection

  • Product: gets the product of all elements in the collection

  • max: gets the maximum value of all elements in the collection

  • min: gets the minimum value of all elements in the collection

demand

  1. Define the Traversable set t1, which contains six numbers from 1 to 6
  2. Count the number of all odd numbers in the t1 set through the count() method, and print the results
  3. Get all the elements and in the t1 set through the sum() method, and print the results
  4. Obtain the product of all elements in the t1 set through the product() method and print the result
  5. Get the maximum value of all elements in the t1 set through the max() method and print the results
  6. Obtain the minimum value of all elements in the t1 set through the min() method, and print the results

Reference code

//Case: demonstrate aggregation operations
object ClassDemo08 {
  def main(args: Array[String]): Unit = {
    //1. Define Traversable set t1, which contains six numbers from 1 to 6
    val t1 = (1 to 6).toTraversable
    //2. Count the number of all odd numbers in t1 set by count() method and print the results
    println(t1.count(_ % 2 == 0))
    println(t1.filter(_ % 2 == 0).size)  //It is not recommended because a new Traversable object will be generated
    //3. Get all the elements and in the t1 set through the sum() method, and print the results
    println(t1.sum)
    //4. Obtain the product of all elements in the t1 set through the product() method and print the result
    println(t1.product)
    //5. Get the maximum value of all elements in t1 set by max() method and print the results
    println(t1.max)
    //6. Obtain the minimum value of all elements in the t1 set through the min() method and print the results
    println(t1.min)
  }
}

2.11 case 9: set type conversion

Sometimes, we need to convert the Traversable set into other sets for operation. At this time, we need to use the toXxx() method

Note: the above Xxx indicates the name of the target set, such as toList, toSet, toArray, toSeq, etc

demand

  1. Define the Traversable set t1, which contains five numbers from 1 to 5
  2. Convert the t1 set into three forms: array, list and set, and print the results

Reference code

//Case: set type conversion
object ClassDemo09 {
  def main(args: Array[String]): Unit = {
    //1. Define Traversable set t1, which contains five numbers from 1 to 5
    val t1 = (1 to 5).toTraversable
    //2. Convert the t1 set into three forms: array, list and set, and print the results
    val arr = t1.toArray
    val list = t1.toList
    val set = t1.toSet
    //3. Print results
    println(arr)
    println(list)
    println(set)
  }
}

2.12 case 10: filling elements

If we need to quickly add the same elements to the collection, for example, to generate five Traversable objects that are all "abc", we need to use the fill() and iterate() methods. If we want to generate queue elements with specified intervals, we can use the range() method, as follows:

  • fill() method: quickly generate a specified number of elements
  • iterate() method: generates a specified number of elements according to the specified conditions
  • range() method: generate all data of a specified interval within a certain interval

demand

  1. Through the fill() method, a Traversable collection is generated, which contains five elements, all of which are "wisdom podcasts"

  2. Through the fill() method, a Traversable set is generated, which contains three random numbers

  3. Generate a Traversable collection through the fill() method

  4. Generate a Traversable set through the iterate() method, which contains five elements: 1, 10, 100, 1000 and 10000

  5. Through the range() method, all data with an interval of 5 from the number 1 to the number 21 are obtained

Reference code

import scala.util.Random

//Case: demonstrating a filler element
object ClassDemo10 {
  def main(args: Array[String]): Unit = {
    //1. Generate a Traversable set through the fill() method, which contains five elements with the value "yueda"
    println(Traversable.fill(5)("yueda"))
    //2. Generate a Traversable set through the fill() method, which contains three random numbers
    println(Traversable.fill(5)(Random.nextInt(100)))
    //3. Generate a Traversable set through the fill() method. The set has five Traversable sets, and each Traversable set has two elements
    //5 means that there are 5 Traversable, and 2 means that each Traversable has 2 elements
    println(Traversable.fill(5, 2)("yueda"))
    //4. Generate a Traversable set through the iterate() method, which contains five elements: 1, 10, 100, 1000 and 10000
    //1 represents the initialization value, and 5 represents the number of elements to be obtained finally
    println(Traversable.iterate(1, 5)(_ * 10))
    //5. Through the range() method, obtain all data with an interval of 5 from the number 1 to the number 21
    println(Traversable.range(1, 21, 5))  //All data from 1 to 20, interval 5
    println(Traversable.range(1, 5))  //All data of interval 1 from 1 to 5 (if not specified, the default interval is 1)
  }
}

3. Case: random student sequence

3.1 requirements

  1. Define a Traversable set, which contains the information of 5 students (the attributes are name and age), and the student's name and age information are randomly generated
    • Assuming that the name information is ("Zhang San", "Li Si", "Wang Wu", "Zhao Liu", "Tian Qi"), the value range of age is: [20, 30), which is written before closing and after opening
  2. Print the results to the console after arranging them in descending order according to the students' age information

3.2 purpose

Investigate the contents related to sample classes, random numbers and sets

3.3 steps

  1. Create a Student sample class with attributes of name and age to record Student information
  2. Define the list and record the student's name information. The values are: "Zhang San", "Li Si", "Wang Wu", "Zhao Liu", "Tian Qi"
  3. Create a random number object r to obtain some random values
  4. Create a Traversable collection containing 5 random student information
  5. Converts a Traversable collection into a List
  6. Through the sortWith() method of the list, it is arranged in descending order according to the age of the students
  7. Print results

3.4 reference code

import scala.util.Random

//Case: random student sequence
object ClassDemo11 {
  //1. Create a Student sample class with attributes of name and age to record Student information
  case class Student(name:String, age:Int)

  def main(args: Array[String]): Unit = {
    //2. Define the list and record the student's name information. The values are: "Zhang San", "Li Si", "Wang Wu", "Zhao Liu", "Tian Qi"
    val names: List[String] = List("Zhang San", "Li Si", "Wang Wu", "Zhao Liu", "pseudo-ginseng")
    //3. Create a random number object r to obtain some random values
    val r: Random = new Random()
    //4. Create a Traversable set containing 5 random student information
    val t1: Traversable[Student] = Traversable.fill(5)(Student(names(r.nextInt(names.size)), 20 + r.nextInt(10)))
    //5. Convert Traversable set into List
    val t2: List[Student] = t1.toList
    //6. Sort the students in descending order according to their ages through the sortWith() method of the list
    //The following two sorting methods are OK
    //val t3 = t2.sortBy(_.age).reverse
    val t3 = t2.sortWith(_.age > _.age)
    //7. Print results
    println(t3)
  }
}

Keywords: Scala Big Data Back-end

Added by Deadman2 on Mon, 06 Dec 2021 07:19:09 +0200