1

i'm writing code for homework and I'm getting this error

GenericSet.java:101: error: method map in class GenericSet<T> cannot be applied
to given types;
            E ans = map(item);
                    ^
  required: LMap<T,E>
  found: T
  reason: cannot infer type-variable(s) E
    (argument mismatch; T cannot be converted to LMap<T,E>)
  where T,E are type-variables:
    T extends Object declared in class GenericSet
    E extends Object declared in method <E>map(LMap<T,E>)
Note: GenericSet.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

from what I understand, the interface I'm being given passes in a function, and I have to use the a generic input(T) to get a generic output (E) which will be stored in a new custom generic object that I made. It looks like it's finding T fine but not E. Could you guys tell me what I'm doing wrong?

Here's my code:

public <E> ExtendedSet<E> map(LMap<T, E> map) {
        GenericSet<E> finalVal = new GenericSet();
        for (T item: this.myList) {
            E ans = map(item);
            finalVal.addThis(ans);
        }
        return finalVal;
    }

Note: the object GenericSet implements ExtendedSet Note2: The interface method im given to implement looks like this:

@FunctionalInterface
public interface LMap<T, E> {
    /**
      *Maps an element of type T to type E
      *@param element the source element to map from
      *@return E the destination element to map to
      */
    E map(T element);
}
1
  • You are trying to pass an instance of T (item) to a method that accepts an instance of LMap<T, E>. Look at your error more closely: required: LMap<T,E> :: found: T Commented Apr 9, 2015 at 2:32

1 Answer 1

1

You want to use

E ans = map.map(item);

A functional interface's method still needs to be invoked via the instance. By omitting the instance, you're calling this.map(item), which takes an LMap<T, E>.

Sign up to request clarification or add additional context in comments.

3 Comments

so is map an object that contains the method map?
@Josephhooper: Exactly. It's an implementation of LMap<T, E>, just like any other interface.
@Josephhooper Don't forget to mark this answer as "accepted answer" by clicking on the checkmark next to the voting arrows! :)

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.