Domanda

Could someone explain why the following code fragmnent does not compile?

public class Main {

    public static void main(String[] args) {

        Integer[] integers = {3, 5, 8};
        Set<Integer> s1 = new HashSet<Integer>(Arrays.asList(integers));

        Double[] doubles = {3.5, 5.5, 8.5};
        Set<Double> s2 = new HashSet<Double>(Arrays.asList(doubles));

        Set<Number> res1 = union(s1, s1);       // ->it does not compile 
        Set<Number> res2 = union(s1, s2);       // ->it does not compile
    }

    static <E> Set<E> union(Set<? extends E> s1, Set<? extends E> s2)
    {
        Set<E> result = new HashSet<E>(s1);
        result.addAll(s2);
        return result;
    }

}

I have read the error messages:

Type mismatch: cannot convert from Set<Integer> to Set<Number>  Main.java   /TestingGenerics/src/com/al/testinggenerics line 17 Java Problem

Type mismatch: cannot convert from Set<Number&Comparable<?>> to Set<Number> Main.java   /TestingGenerics/src/com/al/testinggenerics line 18 Java Problem

What causes the problem as long as Integer and Double extend from Number?

È stato utile?

Soluzione

As mentioned by user3580294 (+1) and in my comment: The reason is that the Java 7 type inference was not smart enough.

However, you may outsmart the type inference by omitting the wildcards while still keeping the desired flexibility:

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class TargetTypeInferenceTest
{
    public static void main(String[] args)
    {

        Integer[] integers = { 3, 5, 8 };
        Set<Integer> s1 = new HashSet<Integer>(Arrays.asList(integers));

        Double[] doubles = { 3.5, 5.5, 8.5 };
        Set<Double> s2 = new HashSet<Double>(Arrays.asList(doubles));

        Set<Number> res1 = union(s1, s1); // ->it does compile ;-)
        Set<Number> res2 = union(s1, s2); // ->it does compile ;-)
    }

    static <E, F extends E, G extends E> Set<E> union(Set<F> s1, Set<G> s2)
    {
        Set<E> result = new HashSet<E>(s1);
        result.addAll(s2);
        return result;
    }
}

Altri suggerimenti

Edit: Wrong answer. My mistake for misinterpreting.

Strangely enough, I get no compile errors in Java 8. You might need to tell the compiler what type E is directly. Try:

Set<Number> res1 = Main.<Number> union(s1, s1);
Set<Number> res2 = Main.<Number> union(s1, s2);

This worked for me using the Java 7 compiler.

My guess is that the compiler in previous versions of Java weren't smart enough to infer the proper type that would make the expression work, so as a result you're forced to tell it explicitly.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top