Search code examples
javascalaapache-commons-math

Constructing an instance of EnumeratedDistribution in Scala


I am having a terrible time doing some trivial: creating a new instance of org.apache.commons.math3.distribution.EnumeratedDistribution in Scala. Best I can make out the following should work...

import org.apache.commons.math3.distribution.EnumeratedDistribution
import org.apache.commons.math3.util.Pair

val p = new Pair(1L, 0.5)
val q = new Pair(2L, 0.5)
val mapping = new java.util.ArrayList[Pair[Long, Double]]()
mapping.add(p)
mapping.add(q)

val dist = new EnumeratedDistribution(mapping)

...this fails with the following error in the REPL...

scala>     val dist = new EnumeratedDistribution[Long](mapping)
<console>:10: error: type mismatch;
 found   : java.util.ArrayList[org.apache.commons.math3.util.Pair[Long,scala.Double]]
 required: java.util.List[org.apache.commons.math3.util.Pair[Long,java.lang.Double]]
       val dist = new EnumeratedDistribution[Long](mapping)

I was under the impression that java.util.List is abstract and thus one must use something like ArrayList instead. I am sure this is something trivial. Thoughts?


Solution

  • Your problem comes from the Double type: java.lang.Double is expected while you use scala.Double.

    Simply try:

    val mapping = new java.util.ArrayList[Pair[Long, java.lang.Double]]()
    

    Then it should work since as you said yourself, ArrayList implements List.