kei1aeh5quahQu4U
kei1aeh5quahQu4U

Reputation: 153

strange Java generics compilation error: parameterized method is not applicable for same parameterized types?

I've stumbled across this error and I can't find the problem:

The method compute(Set<String>, Set<String>) 
in the type JaccardSimilarity<String> is not applicable 
for the arguments (Set<String>, Set<String>)

The method in question is using Generics:

public class JaccardSimilarity<E> implements SimilarityMeasure<Set<E>, Set<E>> {

    @Override
    public double compute(Set<E> s1, Set<E> s2){
        // compute some stuff
    return unionSize == 0 ? 1 : intersectionSize / (double)unionSize;
    }
}

I'm calling it in my class this way:

public MyClass {
    private JaccardSimilarity<String> sim = new JaccardSimilarity<String>();

    public void calc() {
    Set<String> s1 = new HashSet<>();
    s1.add("hallo welt");
    Set<String> s2 = new HashSet<>();
    s2.add("hallo welt");
            // the line below throws the error..
    double result = sim.compute(s1, s2);
    }

In my humble understanding of Java Generics this is perfectly valid code...

How is this possible?

Edit - Additional Code:

public interface SimilarityMeasure<Q, R> extends RelevanceFunction<Q, R> {}

and..

public interface RelevanceFunction<Q, R> {
    public double compute(Q v1, R v2);
}

Edit 2: Here are the Imports:

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;

import <redacted>.JaccardSimilarity;

Edit 3: This was the error:

import <redacted>.representation.Set;

subtle...

Upvotes: 1

Views: 641

Answers (1)

Has QUIT--Anony-Mousse
Has QUIT--Anony-Mousse

Reputation: 77454

You might be importing different classes. Either for Set or String.

Double-check your imports!

Also consider using Set<? extends E> in your implementation. Then E only needs to be a supertype of the actual type you use, if it satisfies the other requirements.

Upvotes: 5

Related Questions