0

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?

1
  • 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? Commented Aug 8, 2015 at 12:28

2 Answers 2

2

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] {
Sign up to request clarification or add additional context in comments.

3 Comments

When I change the override method to override def iterator(): Iterator[Product2[K, V]] I get the following error Error: type mismatch; found : Iterator[Product2[K,C]] required: scala.collection.GenTraversableOnce[Product2[K,V]] partitionedIterator.flatMap(pair => pair._2) ^
@HaseebJaved, did you try to change your with instead as I have mentioned.
yes but that lead to all sorts of other errors like: Error:(90, 22) class ExternalWriter needs to be abstract, since method insertAll in trait Writer of type (x$1: Iterator[Product2[K,C]])Unit is not defined (Note that Iterator[Product2[K,C]] does not match Iterator[Product2[K,V]]: their type parameters differ) private[spark] class ExternalWriter[K, V, C]( I guess I have the structure of my program all wrong and may be I should start anew.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.