1

I am new to scala and spark and have a requirement where i want to use both format and substitution in a single println statement.

Here is the code:

val results = minTempRdd.collect()

for(result <- results.sorted){
  val station = result._1
  val temp = result._2
  println(f" StId $station Temp $temp%.2f F")
}

where results is an RDD having structure (stationId, Temperature).

Now i want to convert this code into one liner. I tried the following code:

val results = minTempRdd.collect()

results.foreach(x => println(" stId "+x._1+" temp = "+x._2))

It works fine, but i am not able to format the second value in tuple here.

Any suggestions, how can we achieve this?

KayV
  • 12,987
  • 11
  • 98
  • 148

1 Answers1

3

The first way is to use curly brackets inside interpolation, which allow to pass arbitrary expressions instead of variables:

println(f" StId ${result._1} Temp ${result._2}%.2fF")

The second way is to unpack the tuple:

for ((station, temp) <- results.sorted) 
  println(f" StId $station Temp $temp%.2fF")

Or:

results.sorted.foreach { case (station, temp) => 
  println(" stId "+x._1+" temp = "+x._2) 
}
Kolmar
  • 14,086
  • 1
  • 22
  • 25
  • println(f" StId ${result._1} Temp ${result._2}%.2fF") This is what i was looking for...Thanks :) – KayV Oct 21 '18 at 10:39