Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,864 questions

51,786 answers

573 users

How to iterate over map in Scala

2 Answers

0 votes
object MyClass {
    def main(args: Array[String]): Unit = {
        val mp = Map("scala" -> 4, "php" -> 2, "c++" -> 7, "python" -> 3, "java" -> 8);
         
        for ((k, v) <- mp) 
            printf("key: %s value: %d\n", k, v);
    }
}
   
   
   
   
/*
run:
   
key: java value: 8
key: c++ value: 7
key: php value: 2
key: scala value: 4
key: python value: 3
   
*/

 



answered Jun 2, 2021 by avibootz
0 votes
object MyClass {
    def main(args: Array[String]): Unit = {
        val mp = Map("scala" -> 4, "php" -> 2, "c++" -> 7, "python" -> 3, "java" -> 8);
          
        mp.keys.foreach{ i =>  
         print("Key: " + i );
         println(" Value: " + mp(i));}
    }
}
    
    
    
    
/*
run:
    
Key: java Value: 8
Key: c++ Value: 7
Key: php Value: 2
Key: scala Value: 4
Key: python Value: 3
    
*/

 



answered Jun 2, 2021 by avibootz

Related questions

3 answers 169 views
3 answers 81 views
81 views asked Mar 3, 2025 by avibootz
1 answer 98 views
3 answers 139 views
1 answer 82 views
3 answers 131 views
1 answer 94 views
...