How to get all the values from a Scala map
Last Updated :
29 Jul, 2019
In order to get all the values from a Scala map, we need to use values method (to get all the values as an Iterable) and if we want to get the values as an iterator, we need to use valuesIterator method. Now, lets check some examples.
Example #1:
object GfG
{
def main(args : Array[String])
{
val m 1 = Map( 3 - > "geeks" , 4 - > "for" , 2 - > "cs" )
val result = m 1 .values
println(result)
}
}
|
Output:
MapLike(geeks, for, cs)
Here, values method is utilized.
Example #2:
object GfG
{
def main(args : Array[String])
{
val m 1 = Map( 3 - > "geeks" , 4 - > "for" , 2 - > "cs" )
val result = m 1 .valuesIterator
println(result)
}
}
|
Output:
non-empty iterator
Here, valuesIterator method is utilized.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...