Java 静态泛型方法的返回类型是否取决于其参数?

Java 静态泛型方法的返回类型是否取决于其参数?,java,generics,Java,Generics,我“简单地”想编写一个静态泛型方法,该方法将任何类型的泛型集合作为其输入,并输出相应类型向量的结果。由于类型E在编译时总是已知的,所以这应该不是问题-但它是。。。因此,以后的调用应该是这样的: Collection<String> coll = ... Vector<String> vec = Convert.toVector(coll); // either this or... Vector<String> vec = Convert<String&

我“简单地”想编写一个静态泛型方法,该方法将任何类型的泛型
集合
作为其输入,并输出相应类型
向量
的结果。由于类型
E
在编译时总是已知的,所以这应该不是问题-但它是。。。因此,以后的调用应该是这样的:

Collection<String> coll = ...
Vector<String> vec = Convert.toVector(coll); // either this or...
Vector<String> vec = Convert<String>.toVector(coll);
集合coll=。。。
向量向量=转换为向量(coll);//不是这个就是。。。
向量向量=转换为向量(coll);
以下是我尝试过的——都没有成功:

import java.util.Collection;
import java.util.Vector;

public class Convert<E> {

    // 1st try, same type E as class => Error: Cannot make a static reference to the non-static type E
    public static Vector<E> toVector1(Collection<E> coll) {
        return new Vector<E>();
    }

    // 2nd try, a new type X. => Error: X cannot be resolved to a type
    public static Vector<X> toVector2(Collection<X> coll) {
        return new Vector<X>();
    }

    // 3rd try, using wildcard. => Error: Cannot instantiate the type Vector<?> 
    public static Vector<?> toVector3(Collection<?> coll) {
        return new Vector<?>();
    }

    // 4th try, using bounded wildcard. => Error: Cannot make a static reference to the non-static type E
    public static Vector<? extends E> toVector4(Collection<? extends E> coll) {
        return new Vector<E>();
    }
}
import java.util.Collection;
导入java.util.Vector;
公共类转换{
//第一次尝试,类型E与class=>Error相同:无法对非静态类型E进行静态引用
公共静态向量到向量1(集合coll){
返回新向量();
}
//第二次尝试,新类型X.=>错误:X无法解析为类型
公共静态向量到向量2(集合coll){
返回新向量();
}
//第三次尝试,使用通配符。=>错误:无法实例化类型向量
公共静态向量到向量3(集合coll){
返回新向量();
}
//第四次尝试,使用有界通配符。=>错误:无法对非静态类型E进行静态引用

publicstaticvector您应该为静态方法提供自己的泛型类型参数:

public static <T> Vector<T> toVector1(Collection<T> coll) {
    return new Vector<T>();
}
公共静态向量到向量1(集合coll){
返回新向量();
}

方法的返回类型之前缺少泛型类型参数声明(
)。

来自JDK文档:“对于静态泛型方法,类型参数部分必须出现在方法的返回类型之前。”。因此

public static <E> Vector<E> toVector1(Collection<E> coll) {
    return new Vector<E>();
}
公共静态向量到向量1(集合coll){
返回新向量();
}
在这里,即使您不使用实例类型参数
E
,但另一个名为
X
,前者的定义不正确。在引入方法范围的类型参数时,您必须执行以下操作:

public static <X> Vector<X> toVector2(Collection<X> coll) {
    return new Vector<X>();
}
公共静态向量到向量2(集合coll){
返回新向量();
}

//第三次尝试,使用通配符。=>错误:无法实例化类型向量
公共静态向量到向量3(集合coll){
返回新向量();
}
这个错误仅仅是因为通配符
只能在返回类型和初始化时使用,而不能在实例化时使用(就像您所做的那样)


//第四次尝试,使用有界通配符。=>错误:无法对非静态类型E进行静态引用

公共静态向量感谢您将我指向官方文档,我真的应该使用RTFM,例如。
// 2nd try, a new type X. => Error: X cannot be resolved to a type
public static Vector<X> toVector2(Collection<X> coll) {
    return new Vector<X>();
}
public static <X> Vector<X> toVector2(Collection<X> coll) {
    return new Vector<X>();
}
// 3rd try, using wildcard. => Error: Cannot instantiate the type Vector<?> 
public static Vector<?> toVector3(Collection<?> coll) {
    return new Vector<?>();
}
// 4th try, using bounded wildcard. => Error: Cannot make a static reference to the non-static type E
public static Vector<? extends E> toVector4(Collection<? extends E> coll) {
    return new Vector<E>();
}
public static <X> Vector<? extends X> toVector4(Collection<? extends X> coll) {
    return new Vector<X>();
}