简体   繁体   English

Java Generics-我可以根据变量类型动态创建列表

[英]Java Generics- Can I create a list dynamically based on variable type

Is there any way by which we can create specific instance of generic type at runtime? 有没有什么方法可以在运行时创建泛型类型的特定实例?

For example. 例如。

Cacheable instance = getCacheable(someInput);

getCacheble method will return me the instance of Cacheable. getCacheble方法将返回Cacheable的实例。 So it can be any class implementing Cacheable.for eg Customer,Product etc.Now I want to create a list of specific type returned by getCacheable as below.Is that possible? 所以它可以是实现Cacheable的任何类。例如Customer,Product等。现在我想创建一个由getCacheable返回的特定类型的列表,如下所示。这可能吗? If yes how to do that? 如果是的话怎么做?

List<? extends Cacheable> cacheList = new ArrayList<>();

I want to create ArrayList<Product> or ArrayList<Customer> based on the instance returned by getCacheable method. 我想基于getCacheable方法返回的实例创建ArrayList<Product> or ArrayList<Customer>

You can do this: 你可以这样做:

Cacheable instance = getCacheable(someInput);
List<? extends Cacheable> l = new ArrayList<>();
l = Collections.checkedList(l, instance.getClass());

Because of type erasure, all information accessible at compile time is lost at runtime. 由于类型擦除,编译时可访问的所有信息在运行时都会丢失。 The checkedList method will ensure that your list will receive only instances of the class of instance variable. checkedList方法将确保您的列表只接收instance变量类的instance

UPDATE: You can do this also: 更新:你也可以这样做:

public static <T extends Cacheable> MyOwnCustomGeneric<T> createMyOwnCustomGeneric(Class<T> type) {
    return new MyOwnCustomGeneric<T>();
}

// ...
IMyOwnCustomGeneric foo = createMyOwnCustomGeneric(instance.getClass());

Use generic helper function: 使用通用帮助函数:

public static <T extends Cacheable>ArrayList<T> createList(Class<T> claz)
{
       return new ArrayList<>();
}

ArrayList<? extends Cacheable>alist = createList(instance.getClass());
if (instance instanceof Product) { 
   // create ArrayList<Product>; 
} else { 
   // create ArrayList<Customer>;
}

This is not really general and dynamic though. 但这并不是一般的和动态的。

The only way to do it is by switching on the type of instance but there isn't much you can do with it: 唯一的方法是打开instance的类型,但是你可以用它做的事情不多:

List<? extends Cacheable> cacheList = null;
if (instance instanceof Product)
  cacheList = new ArrayList<Product>();
// etc

But what can you do with this list? 但你对这份清单怎么办? You cannot add anything to it. 你不能添加任何东西。 Maybe you add items to it while creating it, but that is probably not the use case since it is a cache list. 也许您在创建项目时会向其添加项目,但这可能不是用例,因为它是一个缓存列表。

If you need to add items to it later, see this answer is the right approach. 如果您以后需要向其中添加项目,请参阅此答案是正确的方法。

If you are not going add items later, it does not matter if the type is List<Cacheable> or List<? extends Cacheable> 如果您以后不再添加项目,那么类型是List<Cacheable>还是List<? extends Cacheable>无关紧要List<? extends Cacheable> List<? extends Cacheable>

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

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