简体   繁体   English

如何在Java中将实体转换为对象

[英]How can I convert Entity to Object in java

I define a general List which can add any type elements, define as: List<List<Object>> rows = new ArrayList<>() ; 我定义了一个可以添加任何类型元素的常规List,定义为: List<List<Object>> rows = new ArrayList<>() ; but when I get a list from my service layer, the compile process would throw a incompatible types exception, codes sample as below: 但是当我从服务层获取列表时,编译过程将引发不兼容的类型异常,代码示例如下:

List<List<Object>> rows = new ArrayList<>();
List<ProductEntity> result = searchResponse.getProducts();
rows.add(result);

the exception is: incompatible types: java.util.List<com.shopee.data.webapispec.brandseller.entity.product.ProductEntity> cannot be converted to java.util.List<java.lang.Object> when I run the command " mvn clean install ", anyone knows how to handle it? 例外是: incompatible types: java.util.List<com.shopee.data.webapispec.brandseller.entity.product.ProductEntity> cannot be converted to java.util.List<java.lang.Object>运行命令时, incompatible types: java.util.List<com.shopee.data.webapispec.brandseller.entity.product.ProductEntity> cannot be converted to java.util.List<java.lang.Object>mvn clean install ”,有人知道如何处理吗?

ProductEntity is a sub class of Object . ProductEntityObject的子类。 However List<ProductEntity> is not a subclass of List<Object> . 但是List<ProductEntity>不是List<Object>的子类。

You need to have List<List<? extends Object>> rows = new ArrayList<>(); 您需要List<List<? extends Object>> rows = new ArrayList<>(); List<List<? extends Object>> rows = new ArrayList<>(); to make this work. 使这项工作。

See here for details on Generics and Object Hierarchies 有关泛型和对象层次结构的详细信息,请参见此处

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

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