Joseph hooper
Joseph hooper

Reputation: 1067

Java- Generic Type not found?

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);
}

Upvotes: 1

Views: 972

Answers (1)

Mark Peters
Mark Peters

Reputation: 81134

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>.

Upvotes: 1

Related Questions