0
votes

I have a Java interface Writer defined as following:

public interface Writer<K, V> {

    Iterator<Product2<K, V>> iterator ();
}

And i am trying to implement this interface in a Scala class ExternalWriter which is as following:

private class ExternalWriter[K, V, C]
  extends Logging
  with Writer[K, V] {

    override def iterator(): Iterator[Product2[K, C]] = {
        partitionedIterator.flatMap(pair => pair._2)
  }
}

But when I try to compile this code, I get an error:

Error: Overriding method iterator in trait SortShuffleFileWriter of type ()Iterator[Product2[K,V]]; method iterator has incompatible type override def iterator(): Iterator[Product2[K, C]] = {

How do I fix this?

2
The java.util.Iterator interface is separate from the scala.collection.Iterator trait (in addition to the problem @Codebender mentioned). Did you account for that?Silly Freak

2 Answers

2
votes

Why did you change V to C?

Your override method should be,

override def iterator(): Iterator[Product2[K, V]] = {
    partitionedIterator.flatMap(pair => pair._2)

If you want to use C, then you should implement Writer with C as,

with Writer[K, C] {
0
votes

Try replacing Iterator in your scala class with java.util.Iterator as the scala Iterator and the java Iterator are different.

private class ExternalWriter[K, V, C]
  extends Logging
  with Writer[K, V] {

    override def iterator(): java.util.Iterator[Product2[K, C]] = {
        partitionedIterator.flatMap(pair => pair._2)
  }
}

The above would be the modified code.