Problems with Scala Iterator vs. Java Iterator (a

2019-09-19 06:47发布

问题:

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.