简体   繁体   English

返回通用空列表

[英]Returning a Generic Empty List

How do I return a custom object which uses generic as an empty list? 如何返回使用generic作为空列表的自定义对象?

I have extended the List interface and created my own custom type 我扩展了List接口并创建了自己的自定义类型

public interface MyCustomList<T>
  extends List<T>
{

In a class, I have a method which returns a custom list but I always end up with a compiler error. 在一个类中,我有一个返回自定义列表的方法,但我总是遇到编译器错误。 Basically the default implementation of this method should return an empty list but I cant get it to work since I am encountering below error. 基本上这个方法的默认实现应该返回一个空列表,但我不能让它工作,因为我遇到下面的错误。 'incompatible types' '不兼容的类型'

public MyCustomList<MyCustomBean> getCodes(String code)
{
    return  Collections.<MyCustomList<MyCustomBean>>emptyList();
}

Whats the proper way of sending back a 'generified' empty list implementation? 什么是发回'广泛'空列表实现的正确方法?

Anything wrong with a perfunctory impl? 敷衍了事有什么不对吗?

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

return new MyCustomListImpl<MyCustomBean>();

Collections.emptyList returns a List<T> , whose implementation is hidden . Collections.emptyList返回List<T> ,其实现是隐藏的 Since your MyCustomList interface is an extension of List , there's no way that method can be used here. 由于您的MyCustomList接口是List扩展 ,因此无法在此处使用该方法。

In order for this to work you will need to make an implementation of an empty MyCustomList , in the same way that the core API's Collections implements an empty List implementation, and then use it instead. 为了实现这一点,您需要实现一个空的MyCustomList ,就像核心API的Collections实现一个空的List实现一样,然后使用它。 For example: 例如:

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

Or more accurately, hide the class itself as an implementation detail: 或者更准确地说,将类本身隐藏为实现细节:

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

In your case this is impossible until you will have proper implementation of your interface MyCustomList . 在您的情况下,这是不可能的,直到您将正确实现您的接口MyCustomList

UPD: Collections.emptyList() returns special implementation of List interface, which of course is not convertible to your MyCustomList . UPD: Collections.emptyList()返回List接口的特殊实现,当然它不能转换为MyCustomList

Can't you use Collections.emptyList() for this purpose. 你不能为此目的使用Collections.emptyList() This is type safe and seems to do what you are looking for! 这是类型安全的,似乎做你想要的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM