Question: Hi I am very new to Scala and Spark. I am writing a test to check the integrity of my data. For this I have a Coordinated Matrix and I map it with the ResultMap. Now in my Testing method I need to fetch it from result map and covert the type into Coordinate it raised an Exception in thread 
"main" java.lang.ClassCastException: scala.Some cannot be cast to org.apache.spark.mllib.linalg.distributed.CoordinateMatrix

This is my code .

```
def SinghTest(map:Map[String,Any ]):Boolean={  
      var res:Boolean=false // false

           val connection= DriverManager.getConnection("Connectionstring ")
       val statement = connection.createStatement();
       val rs = statement.executeQuery("select A,B from Demo P" +
           " join Demo_REL  R on p.id=R.ID " +
           "join Cpu CN on CN.id=R.CID" + 
           " limit 10 ");
        /***
       * Maping with ResultMap
       ***/
       val matrix=map.get("MatrixEntries").asInstanceOf[CoordinateMatrix]

       matrix.entries.take(10).foreach(x=> {

         val ph=x.i
         val ch=x.j
         val pid=rs.getLong(1)
         val cid=rs.getLong(2)
         if((ph!=pid)&&ch!=cid)
           throw new Exception("Fail")
         })

```

Answer: 
The `get` method on maps does not return the element directly, but an `Option` of it. This means, for a `Map[String, Any]` the result type is `Option[Any]`. An option can either contain a value or be empty; if your maps contains the key, you will get a `Some` with the value, otherwise a `None`. You can then operate on the value using the methods on `Option` or get it via `getOrElse`, which takes a default value to use if it was a `None`.

```scala
val matrix = map.getOrElse("MatrixEntries", someDefaultMatrix).asInstanceOf[CoordinateMatrix]

```

If you are sure that the map contains the key, you can access the elements directly by using `map(key)`, just leaving out the `.get`. This will give you the element directly, but throw an exception, if the key is not defined for your map.

```scala
val matrix = map("MatrixEntries").asInstanceOf[CoordinateMatrix]

```

PS: Note that using `Any` is usually considered bad style, as it throws away any type safety. If your map contains a mostly fixed set of keys and you have control over its creation (i.e. it's not from a library), look into replacing it with a case class:

```scala
case class TestData(matrixEntries: CoordinateMatrix /* further elements here */)
// ...
val matrix = testData.matrixEntries // no casting required, type errors checked at compile time

```
I did it this way, using the help of @crater2150  

 val matrix=map("MatrixEntries").asInstanceOf[CoordinateMatrix] // But the conversion is required.