The use of multiple collections in Scala (3) methods for iterating through collections

Source: Internet
Author: User
methods for traversing collections 1. Iterating through a collection with a foreach Loop

foreach receives a function as a parameter. The defined function should receive an element as an input parameter, and then do not return anything. The type of the input parameter should match the type in the collection. With the execution of foreach, it passes an element to your function every time, until the last element in the collection.

The most common use of foreach is output information:

scala> val x = vector (x:scala.collection.immutable.vector[int) = vector (1, 2, 3) scala> X.foreach ((i:int) =&G T println (i)) 123

For expressions, there are other formulations:

X.foreach (i = println (i)) X.foreach (println (_)) X.foreach (println)

For the last expression, the case is that a function that consists of only one statement and accepts a parameter can be simply written in this form.

2. Iterating through a collection with a For loop

you can use a for loop to traverse any type of traversable.

scala> val fruits = traversable ("Apple", "orange", "banana") fruits:traversable[string] = List (apple, orange, banana) Scala> for (i <-fruits) println (i) Appleorangebanana

If the algorithm is long, it can be executed in the code block for the for loop.

Here are a few ways to use the counters in the For loop:

scala> val fruits = Array ("Apple", "banana", "orange") fruits:array[string] = array (apple, banana, orange) scala> for (i <-0 until Fruits.size) println (S "$i is ${fruits (i)}") 0 is Apple1 was Banana2 is orange
scala> val fruits = Array ("Apple", "banana", "orange") fruits:array[string] = array (apple, banana, orange) scala> for ((Elem,count) <-Fruits.zipwithindex) {     | println (S "$count is $elem")     |} 0 is Apple1 are Banana2 is orange
scala> val fruits = Array ("Apple", "banana", "orange") fruits:array[string] = array (apple, banana, orange) scala> for ((Elem,count) <-fruits.zip (Stream from 1)) {     | println (S "$count is $elem")     |} 1 is apple2 are Banana3 is orange
3. Create a loop counter with Zipwithindex

(1) iterating through the collection in the form of a foreach

Scala> val days = Array ("Sunday", "Monday", "Tusday", "Wednsday", "Thursday", "Friday", "Saturday") days:array[string] = Array (Sunday, Monday, Tusday, Wednsday, Thursday, Friday,saturday) scala> days.zipwithindex.foreach{| case (Day, count) = println (S "${count+1} is $day") | }1 is Sunday2 are Monday3 is Tusday4 are Wednsday5 is Thursday6

Another way to express it:

Scala> val days = Array ("Sunday", "Monday", "Tusday", "Wednsday", "Thursday", "Friday", "Saturday") days:array[string] = Array (Sunday, Monday, Tusday, Wednsday, Thursday, Friday,saturday) scala> days.zipwithindex.foreach{d=> | println (S "${d._2+1} is ${d._1}") | }1 is Sunday2 are Monday3 is Tusday4 are Wednsday5 is Thursday6

(2) Loop through in the form of for

Scala> val days = Array ("Sunday", "Monday", "Tusday", "Wednsday", "Thursday", "Friday", "Saturday") days:array[string] = Array (Sunday, Monday, Tusday, Wednsday, Thursday, Friday,saturday) scala> for ((day,count) <-Days.zipwithindex) { | println (S "${count+1} is $day") | }1 is Sunday2 are Monday3 is Tusday4 are Wednsday5 is Thursday6
4. Create a loop counter with zip

Using stream in Zip is a way to survive a counter.

Scala> val days = Array ("Sunday", "Monday", "Tusday", "Wednsday", "Thursday", "Friday", "Saturday") days:array[string]  = Array (Sunday, Monday, Tusday, Wednsday, Thursday, Friday,saturday) scala> for ((day,count) <-Days.zip (Stream from 1) {| println (S "$count is $day") |} 1 is Sunday2 are Monday3 is Tusday4 are Wednsday5 are Thursday6 are Friday7
5.range Cycle Counter

If you just need to do something repeatedly, you can use range.

scala> val fruits = Array ("Apple", "banana", "orange") fruits:array[string] = array (apple, banana, orange) scala> for (i <-0 until fruits.size) {| println (S "$i is ${fruits (i)}")}0 are apple1 are Banana2 is orange
6. Using the reduce method to traverse the collection

Use the Reduceleft and Reduceright methods to iterate through the elements in the sequence, pass the adjacent elements to your function into a new result, and then compare the next element of the sequence to generate a new result.

The Reduceleft method is to traverse a sequence from left to right, in which the first two elements are compared and then a result is returned. The result is compared to the third element, followed by a new result, followed by a fourth element comparison, and so on.

Scala> val A = Array (12,3,4,5,67) a:array[int] = Array (3, 4, 5,) scala> A.reduceleft (_ + _) Res4:int = 91scala > A.reduceleft (_ min _) Res5:int = 3scala> A.reduceleft (_ max _) Res6:int = 67

Here are two underscores, which represent the two arguments passed to the function.

Reduceright and Reduceleft the same.

7. Iterating through the collection using the Fold method

The Foldleft method is like Reduceleft, which sets a seed value for the first element. Foldleft receives two parameter lists. The first list has a field, a seed value. The second list is the block of code to run.

Scala> val A = Array (12,3,4,5,67) a:array[int] = Array (3, 4, 5,) scala> A.reduceleft (_ + _) Res8:int = 91scala > A.foldleft (_ + _) Res9:int = 111

Let's take a look at the execution of Foldleft and foldright:

The following/: is a simplified version of Foldleft.

Scala> ((1 to 4). Foldleft (5)) ((i,sum) and i-sum) Res25:int = -5scala> (1 to 4). Foldleft (5) (_-_) Res8:int = -5scala > (5/:(1 to 4)) (_-_) Res9:int = 5

Foldleft Execution Process: 5-1-2-3-4=-5

The following: \ is a simplified version of Foldright.

Scala> ((1 to 4). Foldright (5)) ((i,sum) and i-sum) Res22:int = 3scala> (1 to 4). Foldright (5) (_-_) Res14:int = 3scal A> ((1 to 4): \5) (_-_) Res15:int = 3

Foldright execution Process: 1-(-(+-(5-0))) =3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.