scala data structures: Stream s, View s, thread-safe collections, parallel collections

1 Stream Stream

stream is a collection. This set can be used to store an infinite number of elements, but this infinite number of elements will not be produced at one time, but will be dynamically produced if the interval needs to be used, and the last element follows the lazy rule (ie: to use results are calculated).

Create a Stream object

def numsForm(n: BigInt) : Stream[BigInt] = n #:: numsForm(n + 1)
val stream1 = numsForm(1)

illustrate

The data type stored in the Stream collection is BigInt

numsForm is a custom function, and the function name is specified by the programmer.

The first element of the created set is n , and the rule for subsequent elements is n + 1

The rules for the generation of subsequent elements can be specified by the programmer, such as numsForm(n * 4)...

Using tail, will dynamically generate new elements to the stream collection according to the rules

//create Stream
def numsForm(n: BigInt) : Stream[BigInt] = n #:: numsForm(n + 1)
val stream1 = numsForm(1)
println(stream1) //
//take the first element
println("head=" + stream1.head) //
println(stream1.tail) //
println(stream1) //?

Use map to map elements of stream and do some calculations

//create Stream
def numsForm(n: BigInt) : Stream[BigInt] = n #:: numsForm(n + 1)
def multi(x:BigInt) : BigInt = {
x * x
}
println(numsForm(5).map(multi)) //? (25,?)

2 View View

basic introduction

With the lazy loading feature of Stream, you can also apply the view method to other collections to get a similar effect, with the following features:

1) The view method produces a collection that is always executed lazily.

2) The view does not cache data and needs to be recalculated every time, such as when traversing the View.

def multiple(num: Int): Int = {
num}
def eq(i: Int): Boolean = {
i.toString.equals(i.toString.reverse)
}
//illustrate: did not use view
val viewSquares1 = (1 to 100)
.map(multiple)
.filter(eq)
println(viewSquares1)
//for (x <- viewSquares1) {}
//use view
val viewSquares2 = (1 to 100)
.view
.map(multiple)
.filter(eq)
println(viewSquares2)

3 Thread-safe collections

All thread-safe collections are collections that start with Synchronized

SynchronizedBuffer
SynchronizedMap
SynchronizedPriorityQueue
SynchronizedQueue
SynchronizedSet
SynchronizedStack

4 Parallel Collections

1) In order to make full use of multi-core CPU s, Scala provides parallel collections (different from the previous serial collections) for parallel computing in multi-core environments.

2) The main algorithms used are: Divide and conquer : Divide and conquer algorithm, Scala is implemented through abstraction layers such as splitters (decomposers), combiners (combiners), etc. The main principle is to decompose the computing work into many tasks and distribute them to some processing. processor to complete, and combine their processing results back

Applications

1) Print 1~5

 

2) View threads accessing elements in parallel collections

val result1 = (0 to 100).map{case _ => Thread.currentThread.getName}
val result2 = (0 to 100).par.map{case _ => Thread.currentThread.getName}
println(result1)
println(result2)

5 Operators

 

Posted by arsenalfan14 on Wed, 01 Jun 2022 21:20:38 +0530