简体   繁体   中英

Using scala.collection.immutable.Map inside my java class

I get bellow exception from my java codes.

 java.lang.ClassCastException: scala.collection.immutable.Map$Map1 cannot be cast to java.util.HashMap
    at au.com.vroc.udf.medianUDF.update(medianUDF.java:79)

I am getting error in my spark application when I cast the buffer to HashMap of java.utill. This is my codes:

    public void update(MutableAggregationBuffer buffer, Row input) {
    if (!input.isNullAt(0)) {   

        HashMap currentBuffer=(HashMap) buffer.get(0);//getting exception here
        //HashMap currentBuffer=new HashMap();  
        currentBuffer.put(input.getLong(0), input.getDouble(0));

        //currentBuffer.add(currentMap);

        buffer.update(0, currentBuffer);

    }
}

I guess instead of java hashmap I have to use "scala.collection.immutable.Map$Map1" inside my java class. Can I use any tool in "JavaConversions" namespace.

Anyhep would be appreciated!

Simplest approach would likely be to use Scala Converters .

It should look something like this (not tested, but type-checks):

import scala.collections.JavaConverters

java.util.Map currentBuffer = JavaConverters.mapAsJavaMapConverter(buffer.get(0)).asJava();

Please note that it returns type-parameterized map (ie java.util.Map<K, V> ), not the non-parameterized java.util.HashMap in your example - you might want to alter the rest of your code to work on the parameterized maps for better type safety.

You get java.util.Map you should use getJavaMap method :

java.util.Map<T, U> currentBuffer = (java.util.Map<T, U>) first.getJavaMap(0)

Note that this is not HashMap - initialized value is Encoded on update and decoded on get . To modify it, you have to make a copy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM