当前位置:网站首页>Scala basic tutorial -- 18 -- set (2)
Scala basic tutorial -- 18 -- set (2)
2022-07-04 18:52:00 【Empty.】
Scala Basic course –18– aggregate ( Two )
Chapter goal
- master Iterable Collection related content .
- master Seq Collection related content .
- master Set Collection related content .
- master Map Collection related content .
- Master the case of counting the number of characters .
1. Iterable
1.1 summary
Iterable Represents a set that can be iterated , It inherited Traversable Trait , It is also the parent of other sets . most important of all , It defines the get iterator (iterator) Methods : def iterator: Iterator[A], This is an abstract method , Its concrete implementation class needs to implement this method , So as to achieve The elements in the iterative return set .
1.2 classification
Traversable There are two ways to traverse data :
adopt iterator() Method realization , Iterate over the function of the element .
This way belongs to
Active iteration, We can go through hasNext() Check if there are any elements , And can actively call next() Method to get the element , namely : We can control the iteration process autonomously .adopt foreach() Method realization , The function of traversing elements .
This way belongs to
Passive iteration, We only provide one function , It does not control the process of traversal , namely : The iterative process is controlled by the set itself .
1.3 Case a : Ergodic set
demand
- Define a list , Storage 1, 2, 3, 4, 5 These five numbers .
- adopt iterator() Method to traverse the above list .
- adopt foreach() Method to traverse the above list .
Reference code
// Case study : Traversing set elements .
object ClassDemo01 {
def main(args: Array[String]): Unit = {
//1. Define a list , Storage 1, 2, 3, 4, 5 These five numbers .
val list1 = (1 to 5).toList
//2. adopt iterator() Method to traverse the above list .
val it: Iterator[Int] = list1.iterator
while(it.hasNext) {
val ele = it.next()
println(ele)
if(ele == 3) println(" Additional operations : " + ele * 10)
}
println("-" * 15)
//3. adopt foreach() Method to traverse the above list .
list1.foreach(println(_))
}
}
1.4 Case 2 : Group traversal
If you encounter take Iterable The elements in the object are divided into fixed size groups , Then traverse This demand , You can go through grouped() Method To implement the .
Format
def grouped(size: Int): Iterator[Iterable[A]]
demand
- Define a Iterable aggregate , Storage 1~13 Between all the integers .
- adopt grouped() Method , Yes Iterable Collection in accordance with 5 Elements are grouped as a group , Traverse and print the results .
Reference code
// Case study : demonstration grouped Grouping function .
object ClassDemo02 {
def main(args: Array[String]): Unit = {
//1. Definition Iterable aggregate , Storage 1~13 Number between .
val i1 = (1 to 13).toIterable
//2. adopt grouped Method according to 5 A group of elements , Yes Iterable Set grouping .
val result1 = i1.grouped(5)
//3. Traversing elements , Print the results .
while (result1.hasNext) {
println(result1.next())
}
}
}
1.5 Case three : Generate tuples according to the index
Iterable Every element stored in the collection is indexed , If we want to follow Elements -> Indexes This format , Generate a new set , here , It needs to be used zipWithIndex() Method 了 .
demand
- Define a Iterable aggregate , Storage "A", “B”, “C”, “D”, "E" These five strings .
- adopt zipWithIndex() Method , according to character string -> Index this format , Generate a new set .
- In line with Indexes -> String format , Generate a new set .
- Print the results .
Reference code
// Case study : Generate tuples according to the index
object ClassDemo03 {
def main(args: Array[String]): Unit = {
//1. Define a Iterable aggregate , Storage "A", "B", "C", "D", "E" These five strings .
val i1 = Iterable("A", "B", "C", "D", "E")
//2. adopt zipWithIndex() Method , according to character string -> Index this format , Generate a new set .
val i2 = i1.zipWithIndex //List((A,0), (B,1), (C,2))
//3. In line with Indexes -> String format , Generate a new set .
val i3 = i1.zipWithIndex.map(x => x._2 -> x._1)
//4. Print the results .
println(i2)
println(i3)
}
}
1.6 Case four : Determine whether the set is the same
occasionally , We don't just want to Determine whether the elements in the two sets are all the same , It also requires that the iterative order of these two set elements be consistent , here , You can go through sameElements() Method To meet this requirement .
namely sameElements() The function of the method is : Judge whether the elements contained in the set and the iteration order of the elements are consistent .
demand
- Definition Iterable aggregate list1, contain "A", “B”, "C" These three elements .
- adopt sameElements() Method , Judge list1 and Iterable(“A”, “B”, “C”) Whether the set is the same .
- adopt sameElements() Method , Judge list1 and Iterable(“A”, “C”, “B”) Whether the set is the same .
- Definition Iterable aggregate list2, contain "A", “B”, “C”, "D" These four elements .
- adopt sameElements() Method , Judge list1 and list2 Are they the same? .
Reference code
import scala.collection.mutable
// Case study : Check two Iterable Whether it contains the same elements .
object ClassDemo04 {
def main(args: Array[String]): Unit = {
//1. Definition Iterable aggregate i1, contain "A", "B", "C" These three elements .
val i1 = Iterable("A", "B", "C")
//2. Judge i1 and Iterable("A", "B", "C") Whether the set is the same .
println(i1.sameElements(Iterable("A", "B", "C")))
//3. Judge i1 and Iterable("A", "C", "B") Whether the set is the same .
println(i1.sameElements(Iterable("A", "C", "B")))
//4. Definition Iterable aggregate i2, contain "A", "B", "C", "D" These four elements .
val i2 = Iterable("A", "B", "C", "D")
//5. Judge i1 and i2 Are they the same? .
println(i1.sameElements(i2))
//6. Expand : establish HashSet Collective storage 1, 2, establish TreeSet Collective storage 2, 1, Then judge whether they are the same .
val hs = mutable.HashSet(1, 2)
val ts = mutable.TreeSet(2, 1)
println(hs.sameElements(ts)) // The result is true, because TreeSet Will sort the elements .
}
}
2. Seq
2.1 summary
Seq Traits represent A sequence of elements arranged in a certain order , Sequence is a special iteratable set , Its element characteristics are Orderly ( The access order of elements is consistent ), repeatable , There is an index .
2.2 classification
The inheritance relationship is as follows :

IndexedSeq and LinearSeq yes Seq Sub characteristics of , It represents the two major sub sects of the sequence .
IndexedSeq TraitRepresents the index sequence , be relative to Seq Come on , It does not add additional methods , For random access to elements , It's more effective , Commonly used subsets are : NumericRange, Range, Vector, String etc. .Range aggregate
Range Represents an ordered integer queue , The spacing between each element is the same , For example, odd queue :1, 3, 5, 7, 9, But Fibonacci series 1, 1, 2, 3, 5, 8 It doesn't belong to Range class . Simply speaking , Range Similar to the arithmetic sequence in Mathematics .
NumericRange aggregate
NumericRange Set is a more general arithmetic queue , Can generate Int, BigInteger, Short, Byte And other types of sequences .
Vector aggregate
Vector Is a general immutable data structure , relatively , It takes a little longer to get data , But updating data randomly is much faster than arrays and linked lists .
LinearSeq TraitRepresents a linear sequence , It is realized through linked list , So it's head, tail, isEmpty It is relatively more efficient to implement . Commonly used subsets are : List, Stack, Queue etc. .Stack: Express
Stackdata structure , The element featuresFirst in, then out.For historical reasons , Scala The current library also contains a immutable.Stack, But it is currently marked as deprecated , Because its design is not very elegant , And the performance is not very good , Because the stack will involve a large number of elements in and out , Therefore, the application scenario of immutable stack is relatively small , The most commonly used is variable stack , for example : mutable.Stack, mutable.ArrayStack.
- mutable.Stack: It's through List, That is, the linked list , Additions and deletions quickly , Slow query .
- mutable.ArrayStack: It's through Array, That is, the array method , Quick query , Add or delete slowly .
Queue: Express
queuedata structure , The element featuresfifo.
2.3 Case a : establish Seq aggregate
demand
establish Seq aggregate , Storage elements 1, 2, 3, 4, 5, And print the results .
Reference code
// Case study : establish Seq object .
object ClassDemo01 {
def main(args: Array[String]): Unit = {
//1. establish Seq aggregate , Storage elements 1, 2, 3, 4, 5.
val s1 = (1 to 5).toSeq
//2. Print the results .
println(s1)
}
}
2.4 Case 2 : Get the length and elements
because Seq Every element of the set is indexed , So we can get the corresponding elements directly through the index , And it can go through length() perhaps size() Method to get the length of the collection .
demand
- establish Seq aggregate , Storage elements 1, 2, 3, 4, 5.
- Print the length of the set .
- Get the index as 2 The elements of .
Reference code
// Case study : Get the length and elements
object ClassDemo02 {
def main(args: Array[String]): Unit = {
//1. establish Seq aggregate , Storage elements 1, 2, 3, 4, 5.
val s1 = (1 to 5).toSeq
//2. Print the length of the set .
println(s1.length, s1.size)
println("-" * 15)
//3. Get the index as 2 The elements of .
println(s1(2))
println(s1.apply(2))
}
}
2.5 Case three : Gets the index value of the specified element
Format
Gets the index value of the specified element , We can go through indexOf(), lastIndexOf(), indexWhere(), lastIndexWhere(),indexOfSlice() Method to implement , The specific functions are as follows :
- indexOf: Get the position where the specified element first appears in the list .
- lastIndexOf: Get the position of the last occurrence of the specified element in the list .
- indexWhere: Get the element that meets the condition , The index that first appears in the set .
- lastIndexWhere: Get the element that meets the condition , Index of the last occurrence in the set .
- indexOfSlice: Gets the first occurrence position of the specified subsequence in the set .
Be careful : The above methods are to return the corresponding index after finding the specified data , If this data cannot be found , Then return to -1.
demand
- Definition Seq aggregate , Store the data : 1, 2, 4, 6, 4, 3, 2.
- Get elements 2 The index that first appears in the set , And print .
- Get elements 2 Index of the last occurrence in the set , And print .
- Get... In the collection , Less than 5 The first even index of , And print .
- From the index 2 The position starts to look in the set , Less than 5 The first even index of , And print .
- Get... In the collection , Less than 5 The last even index of , And print .
- Acquiring subsequence Seq(1, 2) stay t1 Collection , First occurrence index , And print .
- From the index 3 Start looking for subsequences Seq(1, 2) stay t1 Collection , First occurrence index , And print .
Reference code
// Case study : Gets the index value of the specified element
object ClassDemo03 {
def main(args: Array[String]): Unit = {
//1. Definition Seq aggregate , Store the data : 1, 2, 4, 6, 4, 3, 2.
val s1 = Seq(1, 2, 4, 6, 4, 3, 2)
//2. Get elements 2 The index that first appears in the set , And print .
println(s1.indexOf(2))
//3. Get elements 2 Index of the last occurrence in the set , And print .
println(s1.lastIndexOf(2))
println("-" * 15)
//4. Get... In the collection , Less than 5 The first even index of , And print .
println(s1.indexWhere(x => (x % 2 == 0) && x < 5))
//5. From the index 2 The position starts to look in the set , Less than 5 The first even index of , And print .
println(s1.indexWhere(x => (x % 2 == 0) && x < 5, 2))
//6. Get... In the collection , Less than 5 The last even index of , And print .
println(s1.lastIndexWhere(x => (x % 2 == 0) && x < 5))
//7. Acquiring subsequence Seq(1, 2) stay t1 Collection , First occurrence index , And print .
println(s1.indexOfSlice(Seq(1, 2)))
//8. From the index 3 Start looking for subsequences Seq(1, 2) stay t1 Collection , First occurrence index , And print .
println(s1.indexOfSlice(Seq(1,2), 3))
}
}
2.6 Case four : Determine whether the specified data is included
If we want to Determine whether the sequence begins or ends with the specified data , And judge whether the sequence contains the specified data , You can go through startsWith(), endsWith(), contains(), containsSlice() To implement the , As follows :
- startsWith: Determine whether the set starts with the specified subsequence .
- endsWith: Determine whether the set ends with the specified subsequence .
- contains: Determine whether the set contains a specified data .
- containsSlice: Determine whether the set contains a specified subsequence .
demand
- Definition Seq aggregate s1, Storage 1 To 10 These ten figures .
- Judge s1 Whether the set is in subsequence (1, 2) start , And print the results .
- Judge s1 Whether the set is in subsequence (1, 3) start , And print the results .
- Judge s1 Whether the set is in subsequence (9, 10) ending , And print the results .
- Judge s1 Whether the set is in subsequence (8, 9) ending , And print the results .
- Judge s1 Whether the collection contains elements 3, And print the results .
- Judge s1 Whether the set contains subsequences Seq(1, 2), And print the results .
- Judge s1 Whether the set contains subsequences Seq(1, 3), And print the results .
Reference code
// Case study : Determine whether the set contains the specified data
object ClassDemo04 {
def main(args: Array[String]): Unit = {
//1. Definition Seq aggregate s1, Storage 1 To 10 These ten figures .
val s1 = (1 to 10).toSeq
//2. Judge s1 Whether the set is in subsequence (1, 2) start , And print the results .
println(s1.startsWith(Seq(1,2)))
//3. Judge s1 Whether the set is in subsequence (1, 3) start , And print the results .
println(s1.startsWith(Seq(1,3)))
//4. Judge s1 Whether the set is in subsequence (9, 10) ending , And print the results .
println(s1.endsWith(Seq(9,10)))
//5. Judge s1 Whether the set is in subsequence (8, 9) ending , And print the results .
println(s1.endsWith(Seq(8,9)))
println("-" * 15)
//6. Judge s1 Whether the collection contains elements 3, And print the results .
println(s1.contains(3))
//7. Judge s1 Whether the set contains subsequences Seq(1, 2), And print the results .
println(s1.containsSlice(Seq(1, 2)))
//8. Judge s1 Whether the set contains subsequences Seq(1, 3), And print the results .
println(s1.containsSlice(Seq(1, 3)))
}
}
2.7 Case 5 : Modify the specified element
If we want to Modify an element value , Or use the specified subsequence to replace a certain element in the set , You can go through updated(), patch() Method to achieve , As follows :
updated: Modify the element of the specified index position to the specified value .
patch: Modify the element of the specified interval to the specified value .
demand
- Definition Seq aggregate s1, Storage 1 To 5 These five data .
- Modify the index 2 The element value of position is : 10, And print the results .
- From the index 1 Start , Use subsequence Seq(10, 20) Replace 3 Elements , And print the results .
Reference code
// Case study : Modify the specified element
object ClassDemo05 {
def main(args: Array[String]): Unit = {
//1. Definition Seq aggregate s1, Storage 1 To 5 These five data .
val s1 = (1 to 5).toSeq
//2. Modify the index 2 The element value of position is : 10, And print the results .
val s2 = s1.updated(2, 10)
println(s2)
//3. From the index 1 Start , Use subsequence Seq(10, 20) Replace 3 Elements , And print the results .
val s3 = s1.patch(1, Seq(10, 20), 3)
println(s3)
}
}
3. Stack
3.1 summary
Express Stack data structure , The element features First in, then out . For historical reasons , Scala The current library also contains a immutable.Stack, But it is currently marked as deprecated , Because its design is not very elegant , And the performance is not very good , Because the stack will involve a large number of elements in and out , Therefore, the application scenario of immutable stack is relatively small , The most commonly used is variable stack , for example : mutable.Stack, mutable.ArrayStack.
3.2 The illustration
Put elements 1, 2, 3 Add to stack , The diagram is as follows :
3.3 Common methods
- top: Get stack top element , But this element will not be removed from the top of the stack .
- push: Indicates the stack operation , It is equivalent to pushing elements into the top of the stack
- pop: Remove the top element , And return this element .
- clear: Remove all elements from the collection .
Be careful :
- immutable.Stack There is a unique method in the collection
pushAll(), Push multiple elements onto the stack .- mutable.ArrayStack The methods unique to the collection are :
- dup(duplicate abbreviation ): It can be used to copy stack top elements .
- preserving: This method will execute an expression , Recover the stack after the expression is executed , namely : The content of the stack is consistent with that before the call .
3.4 Example 1 : demonstration Stack Variable stack
demand
- Define variable stack Stack, Storage 1 To 5 this 5 A digital .
- adopt top() Method to get the top element , And print .
- adopt push() Method to put the element 6 Add to the top of the stack , And print .
- adopt pushAll() Add... To the top of the stack Seq(11, 22, 33) Sequence , And print .
- adopt pop() Method to remove the top element , And print .
- adopt clear() Method to remove all elements in the collection .
Reference code
import scala.collection.mutable
// Case study : demonstration Stack Variable stack
object ClassDemo06 {
def main(args: Array[String]): Unit = {
//1. Define variable stack Stack, Storage 1 To 5 this 5 A digital .
val s1 = mutable.Stack(1, 2, 3, 4, 5)
//2. adopt top() Method to get the top element , And print .
println(s1.top)
println(s1.size)
println("-" * 15)
//3. adopt push() Method to put the element 6 Add to the top of the stack , And print .
s1.push(6)
println(s1)
println("-" * 15)
//4. adopt pushAll() Add... To the top of the stack Seq(11, 22, 33) Sequence , And print .
s1.pushAll(Seq(11, 22, 33))
println(s1)
println("-" * 15)
//5. adopt top() Method to remove the top element , And print .
println(s1.pop())
println(s1.size)
//6. adopt clear() Method to remove all elements in the collection .
s1.clear()
println(s1)
}
}
3.5 Case 2 : demonstration ArrayStack Variable stack
demand
- Define variable stack ArrayStack, Storage 1 To 5 this 5 A digital .
- adopt dup() Method to copy the top element , And print the results .
- adopt preserving() Method realization
First empty the set elements , Then recover the cleared data in the collection, And print .
Reference code
import scala.collection.mutable
// Case study : demonstration ArrayStack Variable stack
object ClassDemo07 {
def main(args: Array[String]): Unit = {
//1. Define variable stack ArrayStack, Storage 1 To 5 this 5 A digital .
val s1 = mutable.ArrayStack(1, 2, 3, 4, 5)
//2. adopt dup() Method to copy the top element , And print the results .
s1.dup()
println(s1)
println("-" * 15)
//3. adopt preserving() Method realization ` First empty the set elements , Then recover the cleared data in the collection `, And print .
s1.preserving({
s1.clear(); println(" See if I've done it !")})
println(s1)
}
}
4. Queue
4.1 summary
Represents a queue , The element features fifo , Our common queue is Variable queue : mutable.Queue, It is internally based on MutableList Data structure .
4.2 The illustration
Put elements 1, 2, 3 Add to queue , The diagram is as follows :

4.3 Common methods
- enqueue: Queue method , You can pass in zero to multiple elements .
- dequeue: Outgoing queue , Remove an element .
- dequeueAll: Remove all elements that meet the conditions .
- dequeueFirst: Remove the first element that meets the condition
4.4 Case presentation
demand
- Define variable queues Queue, Storage 1 To 5 These five data .
- Add elements to the queue 6, And print .
- Add elements to the queue 7, 8, 9, And print .
- Remove the first element of the queue , And print the element .
- Remove the first odd number of the queue , And print the element .
- Remove all even numbers in the queue , And print all removed data .
- Print variable queues Queue, See the end result .
Reference code
import scala.collection.mutable
// Case study : demonstration Queue Sequence .
object ClassDemo08 {
def main(args: Array[String]): Unit = {
//1. Define variable queues Queue, Storage 1 To 5 These five data .
val q1 = mutable.Queue(1, 2, 3, 4, 5)
//2. Add elements to the queue 6, And print .
q1.enqueue(6)
//3. Add elements to the queue 7, 8, 9, And print .
q1.enqueue(7, 8, 9)
println(q1)
println("-" * 15)
//4. Remove the first element of the queue , And print the element .
println(q1.dequeue())
//5. Remove the first odd number of the queue , And print the element .
println(q1.dequeueFirst(_ % 2 != 0))
//6. Remove all even numbers in the queue , And print all removed data .
println( q1.dequeueAll(_ % 2 == 0))
//7. Print variable queues Queue, See the end result .
println(q1)
}
}
5. Set
5.1 summary
Set The elements in the collection do not contain duplicate elements , Common subcategories are : SortedSet( Subclass is TreeSet), HashSet, , ListSet.
5.2 classification
The inheritance relationship is as follows :

- HashSet: Is a prefix tree ( Also called : Dictionary tree ) In the form of , The element features
only , disorder, Often used for statistics . - LinkedHashSet: The element features
only , Orderly. - TreeSet: The element features
only , Sort.
5.3 Example
demand
- establish SortedSet aggregate , Storage elements 1, 4, 3, 2, 5, Then print the set .
- establish HashSet aggregate , Storage elements 1, 4, 3, 2, 5, Then print the set .
- establish LinkedHashSet aggregate , , Storage elements 1, 4, 3, 2, 5, Then print the set .
Reference code
import scala.collection.{
SortedSet, mutable}
// Case study : demonstration Set aggregate .
object ClassDemo09 {
def main(args: Array[String]): Unit = {
//1. establish SortedSet aggregate , Storage elements 1, 4, 3, 2, 5, Then print the set .
val s1 = SortedSet(1, 4, 3, 2, 5)
println(s1)
println("-" * 15)
//2. establish HashSet aggregate , Storage elements 1, 4, 3, 2, 5, Then print the set .
val s2 = mutable.HashSet(1, 4, 3, 2, 5)
println(s2)
println("-" * 15)
//3. establish LinkedHashSet aggregate , , Storage elements 1, 4, 3, 2, 5, Then print the set .
val s3 = mutable.LinkedHashSet(1, 4, 3, 2, 5)
println(s3)
}
}
6. Map
6.1 summary
Map Represent mapping , It contains key value pairs (key-value) Set , Map The basic operation of type is similar to Set Operations on collections , Because it contains elements Entry It's a key value pair , therefore Map Provides some methods for key or value operations alone .
6.2 classification
The inheritance relationship is as follows :

6.3 Example
demand
Definition Map aggregate , Store data as : “A” -> 1, “B” -> 2, “C” -> 3.
Traverse Map aggregate .
adopt filterKeys() Method , Get the output key as "B" This set of key values of the object , And print the results .
filterKeys: Filter for keys , Get the key value pairs that meet the conditions according to the key .
Reference code
// Case study : demonstration Map aggregate .
object ClassDemo10 {
def main(args: Array[String]): Unit = {
//1. Definition Map aggregate , Store data as : "A" -> 1, "B" -> 2, "C" -> 3.
val m1 = Map("A" -> 1, "B" -> 2, "C" -> 3)
//2. Traverse Map aggregate .
m1.foreach(println(_))
println("-" * 15)
//3. adopt filterKeys() Method , Get the output key as "B" This set of key values of the object , And print the results .
println(m1.filterKeys(_ == "B"))
}
}
7. Case study : Count the number of characters
7.1 demand
- Prompt the user to enter a string , And receive .
- Count the number of occurrences of each character in the above string , And print the results to the console .
7.2 Purpose
A comprehensive investigation aggregate , Keyboard Entry Related knowledge .
7.3 step
- Prompt the user to enter a string , And receive .
- Definition Map aggregate , Used to store characters and their occurrence times . key : character , value : The number of times characters appear .
- Convert string to character array .
- Traversal character array , Get every character .
- If the character appears for the first time , Record the number as 1, If the character is repeated , Just count the number of times +1, Then re store .
- Ergodic set , View results .
7.4 Reference code
import scala.collection.mutable
import scala.io.StdIn
// Case study : Count the number of characters .
object ClassDemo11 {
def main(args: Array[String]): Unit = {
//1. Prompt the user to enter a string , And receive .
println(" Please enter a string : ")
val s = StdIn.readLine()
//2. Definition Map aggregate , Used to store characters and their occurrence times . key : character , value : The number of times characters appear .
val m1 = mutable.Map[Char,Int]()
//3. Convert string to character array .
val chs = s.toCharArray
//4. Traversal character array , Get every character .
for(k <- chs) {
//5. If the character appears for the first time , Record the number as 1, If the character is repeated , Just count the number of times +1, Then re store .
m1 += (k -> (if(m1.contains(k)) m1.getOrElse(k, 1) + 1 else 1))
}
//6. Ergodic set , View results .
for((k, v) <- m1) {
println(k, v)
}
}
}
Recurring , Just count the number of times +1, Then re store .
6. Ergodic set , View results .
7.4 Reference code
import scala.collection.mutable
import scala.io.StdIn
// Case study : Count the number of characters .
object ClassDemo11 {
def main(args: Array[String]): Unit = {
//1. Prompt the user to enter a string , And receive .
println(" Please enter a string : ")
val s = StdIn.readLine()
//2. Definition Map aggregate , Used to store characters and their occurrence times . key : character , value : The number of times characters appear .
val m1 = mutable.Map[Char,Int]()
//3. Convert string to character array .
val chs = s.toCharArray
//4. Traversal character array , Get every character .
for(k <- chs) {
//5. If the character appears for the first time , Record the number as 1, If the character is repeated , Just count the number of times +1, Then re store .
m1 += (k -> (if(m1.contains(k)) m1.getOrElse(k, 1) + 1 else 1))
}
//6. Ergodic set , View results .
for((k, v) <- m1) {
println(k, v)
}
}
}
边栏推荐
- uni-app与uviewUI实现仿小米商城app(附源码)
- 2022年字节跳动日常实习面经(抖音)
- Scala基础教程--20--Akka
- With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute
- 如何使用 wget 和 curl 下载文件
- ISO27001 certification process and 2022 subsidy policy summary
- Halcon template matching
- How is the entered query SQL statement executed?
- Digital "new" operation and maintenance of energy industry
- Pb extended DLL development (super chapter) (VII)
猜你喜欢

力扣刷题日记/day6/6.28

线上MySQL的自增id用尽怎么办?

2022 national CMMI certification subsidy policy | Changxu consulting

华为云ModelArts的使用教程(附详细图解)

Once the "king of color TV", he sold pork before delisting

Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers

基于unity的愤怒的小鸟设计
![[HCIA continuous update] WAN technology](/img/31/8e9ed888d22b15eda5ddcda9b8869b.png)
[HCIA continuous update] WAN technology

力扣刷题日记/day3/2022.6.25

Machine learning concept drift detection method (Apria)
随机推荐
Thawte通配符SSL证书提供的类型有哪些
Crawler (6) - Web page data parsing (2) | the use of beautifulsoup4 in Crawlers
vbs或vbe如何修改图标
Mxnet implementation of googlenet (parallel connection network)
机器学习概念漂移检测方法(Aporia)
LD_ LIBRARY_ Path environment variable setting
DB engines database ranking in July 2022: Microsoft SQL Server rose sharply, Oracle fell sharply
mysql5.7安装教程图文详解
力扣刷題日記/day6/6.28
How to improve development quality
2022 national CMMI certification subsidy policy | Changxu consulting
Li Kou brush question diary /day8/7.1
Scala基础教程--12--读写数据
如何使用 wget 和 curl 下载文件
File processing examples of fopen, FREAD, fwrite, fseek
Uni app and uviewui realize the imitation of Xiaomi mall app (with source code)
Grain Mall (I)
Scala基础教程--13--函数进阶
Lua EmmyLua 注解详解
Halcon template matching