Problems with Scala Iterator vs. Java Iterator (a casting nightmare): How to cast to Java from Scala?

好久不见. 提交于 2020-02-07 07:56:15

问题


What I am trying to accomplish is simple: Return the list containing a Tuple2 as a Java Iterator.

The code snippet is:

....public java.util.Iterator<Tuple2<Path, Text>> call(InputSplit inputSplit, java.util.Iterator<Tuple2<LongWritable, Text>> iter) throws Exception { 


    .....  java.util.List<Tuple2<Path, Text>>  elements = new java.util.ArrayList<Tuple2<Path, Text>>();
        while(iter.hasNext()){
            Tuple2<LongWritable,Text> tupled = iter.next();
            Tuple2<Path, Text> toAdd = new Tuple2<Path, Text>(file.getPath(),tupled._2);
            elements.add(toAdd);
        }
        java.util.Iterator<Tuple2<Path, Text>> it = elements.iterator();
        return it; .....}

Note: the variable iter is also a Java Iterator.

The error I get is: java.lang.ClassCastException: java.util.ArrayList$Itr cannot be cast to scala.collection.Iterator

  1. What am I doing wrong? (I am aware that I am using Tuple2, which is meant for Scala).
  2. How do I return a Java Iterator with a Tuple2 or a Tuple2 equivalent ?

Thank you


回答1:


Looks like you're doing an illegal cast from Java iterator to Scala iterator.

Try using scala.collection.JavaConversions. When you import this implicit class - these conversions will be done automatically.

In short -

import scala.collection.JavaConversions._

Another option is to do it explicitly, assuming javaIterator is your java iterator -

import scala.collection.JavaConverters._
javaIterator.asScala



回答2:


Maxim answer is correct, here is exact java code which will do conversion:

import scala.collection.JavaConversions;
List<String> javaBatch = new ArrayList<String>();
scala.collection.Iterable<String> scalaBatch =  JavaConversions.asScalaIterable(javaBatch);

Similarly, method JavaConversions.asScalaIterator() can be used for iterators. Vice-versa is also possible.



来源:https://stackoverflow.com/questions/29629763/problems-with-scala-iterator-vs-java-iterator-a-casting-nightmare-how-to-cas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!