I have a List with Strings and I want to print the longest String out of it
I have tried the reduceLeft
option but whenever I am applying it its returning this error:
type mismatch; found:String required:Ordering[?]
Here is the code throwing the exception in the second line:
val input2 = List("one", "two", "three", "four", "five")
for (entry <- input2.reduceLeft(_ max _)) println(input2.max)
you should ask yourself: What is the max for a list of Strings?
List[String].max
and String.max
need more clarification for their task.
use List.maxBy
instead of List.reduceLeft
input2.maxBy(_.length)
use implicit type conversion
implicit def string2ordering(s: String) = s.length
input2.max
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With