Java 返回泛型空列表

Java 返回泛型空列表,java,generics,Java,Generics,如何返回使用泛型作为空列表的自定义对象 我扩展了列表接口并创建了自己的自定义类型 public interface MyCustomList<T> extends List<T> { 公共接口MyCustomList 扩展列表 { 在一个类中,我有一个方法返回一个自定义列表,但我总是以一个编译器错误结束。基本上,这个方法的默认实现应该返回一个空列表,但我不能让它工作,因为我遇到下面的错误。 “不兼容类型” public MyCustomList<MyCust

如何返回使用泛型作为空列表的自定义对象

我扩展了列表接口并创建了自己的自定义类型

public interface MyCustomList<T>
  extends List<T>
{
公共接口MyCustomList
扩展列表
{
在一个类中,我有一个方法返回一个自定义列表,但我总是以一个编译器错误结束。基本上,这个方法的默认实现应该返回一个空列表,但我不能让它工作,因为我遇到下面的错误。 “不兼容类型”

public MyCustomList<MyCustomBean> getCodes(String code)
{
    return  Collections.<MyCustomList<MyCustomBean>>emptyList();
}
public MyCustomList getCodes(字符串代码)
{
返回集合。emptyList();
}

发送回“一般化”空列表实现的正确方法是什么?

在您的情况下,这是不可能的,除非您将正确实现接口
MyCustomList


UPD:
Collections.emptyList()
返回
列表
接口的特殊实现,该接口当然不能转换为您的
MyCustomList
在您的情况下,在正确实现接口之前,这是不可能的


UPD:
Collections.emptyList()
返回
列表
接口的特殊实现,该接口当然不能转换为您的
MyCustomList

敷衍了事的实现有什么问题吗

class MyCustomListImpl<T> extends ArrayList<T> implements MyCustomList<T> {}

return new MyCustomListImpl<MyCustomBean>();
类MyCustomListImpl扩展ArrayList实现MyCustomList{} 返回新的MyCustomListImpl();
敷衍的执行有什么问题吗

class MyCustomListImpl<T> extends ArrayList<T> implements MyCustomList<T> {}

return new MyCustomListImpl<MyCustomBean>();
类MyCustomListImpl扩展ArrayList实现MyCustomList{} 返回新的MyCustomListImpl();
集合。emptyList
返回一个
列表
,其实现是。由于您的
MyCustomList
接口是
列表
的扩展,因此无法在此处使用该方法

为了实现这一点,您需要实现空的
MyCustomList
,就像核心API的
集合实现空的
List
实现一样,然后使用它。例如:

public final class MyEmptyCustomList<T> extends AbstractList<T> implements MyCustomList<T> {

    private static final MyEmptyCustomList<?> INSTANCE = new MyEmptyCustomList<Object>();

    private MyEmptyCustomList() { }

    //implement in same manner as Collections.EmptyList

    public static <T> MyEmptyCustomList<T> create() {

        //the same instance can be used for any T since it will always be empty
        @SuppressWarnings("unchecked")
        MyEmptyCustomList<T> withNarrowedType = (MyEmptyCustomList<T>)INSTANCE;

        return withNarrowedType;
    }
}
public final类MyEmptyCustomList扩展了AbstractList实现了MyCustomList{
私有静态最终MyEmptyCustomList实例=新建MyEmptyCustomList();
私有MyEmptyCustomList(){}
//以与Collections.EmptyList相同的方式实现
公共静态MyEmptyCustomList创建(){
//同一实例可用于任何T,因为它总是空的
@抑制警告(“未选中”)
MyEmptyCustomList WithSlowedType=(MyEmptyCustomList)实例;
返回窄类型;
}
}
或者更准确地说,隐藏类本身作为实现细节:

public class MyCustomLists { //just a utility class with factory methods, etc.

    private static final MyEmptyCustomList<?> EMPTY = new MyEmptyCustomList<Object>();

    private MyCustomLists() { }

    private static final class MyEmptyCustomList<T> extends AbstractList<T> implements MyCustomList<T> {
        //implement in same manner as Collections.EmptyList
    }

    public static <T> MyCustomList<T> empty() {
        @SuppressWarnings("unchecked")
        MyCustomList<T> withNarrowedType = (MyCustomList<T>)EMPTY;
        return withNarrowedType;
    }
}
公共类MyCustomList{//只是一个带有工厂方法等的实用程序类。
private static final MyEmptyCustomList EMPTY=new MyEmptyCustomList();
私有MyCustomList(){}
私有静态最终类MyEmptyCustomList扩展了AbstractList,实现了MyCustomList{
//以与Collections.EmptyList相同的方式实现
}
公共静态MyCustomList empty(){
@抑制警告(“未选中”)
MyCustomList WithSlowedType=(MyCustomList)为空;
返回窄类型;
}
}

集合。emptyList
返回一个
列表
,其实现是。由于您的
MyCustomList
接口是
列表
的扩展,因此无法在此处使用该方法

为了实现这一点,您需要实现空的
MyCustomList
,就像核心API的
集合实现空的
List
实现一样,然后使用它。例如:

public final class MyEmptyCustomList<T> extends AbstractList<T> implements MyCustomList<T> {

    private static final MyEmptyCustomList<?> INSTANCE = new MyEmptyCustomList<Object>();

    private MyEmptyCustomList() { }

    //implement in same manner as Collections.EmptyList

    public static <T> MyEmptyCustomList<T> create() {

        //the same instance can be used for any T since it will always be empty
        @SuppressWarnings("unchecked")
        MyEmptyCustomList<T> withNarrowedType = (MyEmptyCustomList<T>)INSTANCE;

        return withNarrowedType;
    }
}
public final类MyEmptyCustomList扩展了AbstractList实现了MyCustomList{
私有静态最终MyEmptyCustomList实例=新建MyEmptyCustomList();
私有MyEmptyCustomList(){}
//以与Collections.EmptyList相同的方式实现
公共静态MyEmptyCustomList创建(){
//同一实例可用于任何T,因为它总是空的
@抑制警告(“未选中”)
MyEmptyCustomList WithSlowedType=(MyEmptyCustomList)实例;
返回窄类型;
}
}
或者更准确地说,隐藏类本身作为实现细节:

public class MyCustomLists { //just a utility class with factory methods, etc.

    private static final MyEmptyCustomList<?> EMPTY = new MyEmptyCustomList<Object>();

    private MyCustomLists() { }

    private static final class MyEmptyCustomList<T> extends AbstractList<T> implements MyCustomList<T> {
        //implement in same manner as Collections.EmptyList
    }

    public static <T> MyCustomList<T> empty() {
        @SuppressWarnings("unchecked")
        MyCustomList<T> withNarrowedType = (MyCustomList<T>)EMPTY;
        return withNarrowedType;
    }
}
公共类MyCustomList{//只是一个带有工厂方法等的实用程序类。
private static final MyEmptyCustomList EMPTY=new MyEmptyCustomList();
私有MyCustomList(){}
私有静态最终类MyEmptyCustomList扩展了AbstractList,实现了MyCustomList{
//以与Collections.EmptyList相同的方式实现
}
公共静态MyCustomList empty(){
@抑制警告(“未选中”)
MyCustomList WithSlowedType=(MyCustomList)为空;
返回窄类型;
}
}

您不能为此目的使用
集合.emptyList()
。这是类型安全的,似乎符合您的要求!

您不能使用
集合.emptyList()吗
用于此目的。这是类型安全的,似乎符合您的要求!

MyCustomList
只是一个接口。在您的决定中,您必须在
return
语句中的匿名类中实现所有
列表
接口。@andemoniy oops-没有注意到这一点。已修复。:)使用此答案,但另一个答案对我来说也很酷。谢谢!
MyCustomList
只是一个接口。在你的决定中,你必须在
return
语句中的匿名类中实现所有
List
接口。@Andremoniy oops-没有注意到。修复了。:)同意了这个答案,但其他答案对我来说也很酷。谢谢!你为什么需要custom列表界面?不是答案,而是解释:您不能这样做的原因是
cole的签名