UnsupportedOperationException: empty.max

Dear All
Excuse me if this question is not a good one. I am new to scala
I am having the error :UnsupportedOperationException: empty.max. I must have empty arrays as a result of a test. And according to my purpose, I will have many of these empty arrays.
Would you kindly help me solve this issue? My whole thesis depends on this.


Well, don’t call max on an empty collection.

val max = if (list.nonEmpty) Some(list.max) else None

Or if you want to have some fun:

scala> :paste
// Entering paste mode (ctrl-D to finish)

import scala.collection.GenTraversableOnce

implicit class SafeMaxSyntax[T, A](t: T)(implicit ev: T => GenTraversableOnce[A], ord: Ordering[A]) {
  def safeMax: Option[A] = {
    val coll = ev(t)
    if (coll.nonEmpty) Some(coll.max) else None

// Exiting paste mode, now interpreting.

import scala.collection.GenTraversableOnce
defined class SafeMaxSyntax

scala> Array(1,2,3).safeMax
res3: Option[Int] = Some(3)

scala> Array[Int]().safeMax
res4: Option[Int] = None

scala> List[Int]().safeMax
res5: Option[Int] = None

scala> "Hello World".safeMax
res6: Option[Char] = Some(r)
1 Like

Excuse me if this is not an acceptable question but, What is the result of empty.max? Is it a zero? a Nan?
This is the code, but if I put None as the result, it cannot make “<=” on the same val. No?

 val maxCategoriesPerFeature = numBins.values.max
          val maxCategory = numBins.find(_._2 == maxCategoriesPerFeature).get._1
          require(maxCategoriesPerFeature <= maxPossibleBins,
            s"FuzzyDecisionTree requires maxBins (= $maxPossibleBins) to be at least as large as the " +
            s"number of values in feature, when split is set to binary, but feature $maxCategory " +
            s"has $maxCategoriesPerFeature values. Considering remove this and other " +
            s"features with a large number of values, or add more training examples.\n" +
            s"For values, we refer to fuzzy sets and categories in case of " + 
             "continuous and categorical features respectively")

The result is an exception being thrown, because an empty list cannot have a max value (since it has no values). So you have to make sure your list isn’t empty before you call max. A Nan would make no sense, because max doesn’t have to be based on a numeric comparison. Similarly a zero.

So the point is, calling max on an empty collection gives you a computation that cannot sensibly be completed, hence you see the exception.

What @millross said.

If you return an Option instead of letting max throw an exception, you can use higher order functions to operate on that optional result. For instance:

val value = numBins.values
val maxCategoriesPerFeature = if (values.nonEmpty) Some(values.max) else None
val maxCategoryOpt = maxCategoriesPerFeature.flatMap { max => 
  numBins.find(_._2 == max)
require(maxCategoriesPerFeature.map(_ <= maxPossibleBins).getOrElse(false))

If you think that in your case there is a sensible result possible for empty.max you can just do

val maxCategoriesPerFeature = if (values.nonEmpty) values.max else mySensibleResult

and continue as normal.

1 Like

The problem is that .max should return Option[A], as should .head, .tail, .reduce, and others. This is a design error that was discussed in the collections redesign but was rejected because reasons.

1 Like