Skip to content

Instantly share code, notes, and snippets.

Amine SAGAAMA AmineSagaama

Block or report user

Report or block AmineSagaama

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@AmineSagaama
AmineSagaama / map.scala
Created Sep 3, 2018
How to traverse a Map in Scala with foreach
View map.scala
val items = Map("A" -> 100, "B" -> 200, "C" -> 300, "D" -> 400, "E" -> 500, "F" -> 600)
// The following approach shows how to use the Tuple syntax to display the key and value of each item
//Output : key: E, value: 500 key: F, value: 600 key: A, value: 100 key: B, value: 200 key: C, value: 300 key: D, value: 400
items.foreach(item => println(s"key: ${item._1}, value: ${item._2}"))
// To display the list of keys in the Map
//Output : E F A B C D
items.keys.foreach(println)
View foreach.scala
val items = List("A", "B", "C")
//method reference
//Output : A,B,C
items.foreach(println)
//Output : A
items.foreach(item => if ("A".equals(item))
println(item))
You can’t perform that action at this time.