scala迭代器_如何在Scala中迭代Map?

scala迭代器

Iterating/ traversing Maps is the process of accessing all elements of the map. For this, we will loop over all elements of the map and print the key-value pairs of the elements of the map.

迭代/遍历地图是访问地图所有元素的过程。 为此,我们将遍历地图的所有元素,并打印地图的元素的键值对。

A map is a data structure that contains data elements in the form of key-value pairs and an element can be accessed using the key.

映射是一种数据结构,其中包含键-值对形式的数据元素,并且可以使用键访问元素。

Syntax for creating map:

创建地图的语法:

    val map_element = Map( key1 -> value1 , key2 -> value2 ) 

Sample Map:

样本图:

    val programmingLang = Map(1 -> "C", 2 -> "C++", 3 -> "Scala"); 

遍历地图 (Iterating over Map)

You can iterate over a map and excess its elements in multiple ways:

您可以通过多种方式遍历地图并超出其元素:

  1. Using for loop

    使用for循环

  2. Using foreach Loop

    使用foreach循环

  3. Using iterator method

    使用迭代器方法

1)使用for循环遍历Scala中的地图 (1) Iterating over the map in Scala using the for loop)

The for loop is used to loop over elements of the map.

for循环用于循环映射图元素。

Syntax:

句法:

    for((key,value) 

Program to print elements of a map using for loop

Output

Iterating over Map using for Loop
1 -> C
2 -> C++
3 -> Scala
4 -> Python

Explanation:

In this program, we have illustrated the use of for loop for iterating over a map. We have created a map named progLang and then using the for loop we have to print the key and value of the map.

2) Iterating over the map in Scala using the foreach loop

The foreach loop is called using the map name and it iterates over all the elements of the map.

Syntax:

There can be two different methods to use the foreach loop for accessing the elements of the map. They match expression and tuple based access.

In match expression based accessing we will be accessing the elements of the map are named and accessed suing it.

Syntax:

    Map_name.foreach{
        case(key, value) => //Code for accessing the values...
    }

The other method is quite each as it uses a tuple to access elements of the map. So, the key will be the first value of tuple, and value will be the second value of the tuple.

Syntax:

Program to print elements of a map using foreach loop

object MyObject {
    def main(args: Array[String]) {
        val myBikes = Map(1 -> "ThunderBird350" , 2 -> "Yamaha R3" , 3 -> "Iron 883" , 4 -> "BMW S1000RR")

        println("Iterating over Map using foreach Loop")
        myBikes.foreach(bike => println("Bike No. : " + bike._1 + " name : " + bike._2))
    }
}

Output

Explanation:

In this program, we have illustrated the use of foreach loop for iterating over a map. We have created a map named myBikes and then used the foreach to iterate over it, using the tuple access method.

3) Iterating over the map in Scala using the iterator method

The iterator method is used to create an iterator for the map that is used to loop over the values of the map.

Syntax:

    map_name.iterator 

It returns an iterator based on the map. Then we have to use iterator_name.next method to print the elements of the iterator which are returned as tuples in case of the map.

Program to print elements of a map using iterator method

Output

Iterating over Map using iterator method
(1,ThunderBird350)
(2,Yamaha R3)
(3,Iron 883)
(4,BMW S1000RR)

Explanation:

In this program, we have illustrated the use of iterator method for iterating over a map. We have created a map and then defined an iterator method that will be used to iterate over the elements of the map. The method itorator.hasnext is used to check if the next element of the iterator is present and the print the element as a tuple using iterator.next method.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.


翻译自: https://www.includehelp.com/scala/how-to-iterate-map-in-scala.aspx

scala迭代器

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值