如何将类对象转换为泛型对象类型?

时间:2022-09-01 17:14:33

I'm working on a method to get some data from MySQL database using NamedParameterJdbcTemplate. The listCurrentRecords should return List of Objects types of either Customer, Product and SalesOrder from database. The objectType is defined from the parameter passed inside the method (1 || 2 || 3) and it is defined earlier in the class as public variable.

我正在研究一种方法,使用NamedParameterJdbcTemplate从MySQL数据库中获取一些数据。listCurrentRecords应该从数据库返回客户、产品和SalesOrder的对象类型列表。objectType是从方法内部传递的参数(1 || 2 ||)定义的,它在类的前面被定义为public变量。

public static final int TYPE_PRODUCT = 1;
public static final int TYPE_CUSTOMER = 2;
public static final int TYPE_SALESORDER = 3;

The method is provided below.

方法提供如下。

public static List<Object> listCurrentRecords(int objectType)
{

    // PRODUCT 
    if ( objectType == 1 ){

    }

    //CUSTOMER 
    else if ( objectType ==  2 ){

    }

    // SALESORDER
    else if ( objectType ==  3 ){

    }

    return null; 
    // return new ArrayList<Object>();
}

Say, the objectType == 2, then it will require to grab some data from the Customer table using getMyCustomer method ( same goes for the Product and SalesOrder, they will use their separate methods ) as following,

例如,objectType == 2,然后需要使用getMyCustomer方法从Customer表中获取一些数据(产品和SalesOrder也是如此,它们将使用各自的方法),如下所示,

public static List<Customer> getMyCustomer(){

    return jdbc.query("select * from Customer", new RowMapper<Customer>() {

        public Customer mapRow(ResultSet rs, int rowNum) throws SQLException {

            Customer customer = new Customer();

            customer.setCustomerID(rs.getString("CustomerID"));
            customer.setName( rs.getString("Name"));
            customer.setAddress( rs.getNString("Address"));
            customer.setPhone1(rs.getNString("Phone 1"));
            customer.setPhone2(rs.getNString("Phone 2"));

            customer.setCreditLimit(rs.getDouble("Credit Limit"));
            customer.setCurrentCredit(rs.getDouble("Current Credit"));

            return customer;
        }
    });

Inside the else if ( objectType == 2 ){ } I would like to call getMyCustomer method and get List<Customer> there. However, the return type of the method listCurrentRecords is List<Object>. How to convert from List<Customer> to List<Object>. I provided the pseudo code as following,

在else if (objectType == 2){}中,我想调用getMyCustomer方法并在那里获取List 。但是,方法listCurrentRecords的返回类型是List 。如何将List 转换为List 。我提供的伪代码如下,

   // customer 
    else if ( objectType ==  2 ){

         List<Customer> myCustomer =  getMyCustomer();
         // how to convert ***myCustomer*** to List<Object> ?
    }

I appreciate some assistance of how to write it properly in Java.

我很感激您对如何用Java正确地编写它的帮助。

3 个解决方案

#1


2  

Since you want the Object type (remember generics are a compile time type checking feature), you can use addAll or the constructor that takes a second List. Like

由于需要对象类型(请记住泛型是一个编译时类型检查特性),所以可以使用addAll或获取第二个列表的构造函数。就像

List<Object> myCustomer = new ArrayList<>(getMyCustomer());

or

List<Object> al = new ArrayList<>();
// ...
al.addAll(getMyCustomer());

#2


1  

I would probably do

我可能会做

List<Object> myCustomer = new ArrayList<>(getMyCustomer());

This creates a new list by iterating over the original, so could be slow depending on the size of the list.

这通过遍历原始列表创建一个新的列表,因此根据列表的大小可能会比较慢。

If this is a problem, an alternative is to do

如果这是一个问题,另一种选择就是去做

List<Object> myCustomer = Collections.unmodifiableList(getMyCustomer());

This version works by wrapping the original list. If you do it this way you will not be able to call any methods that modify the list (add, set, remove, clear etc).

这个版本通过封装原始列表来工作。如果您这样做,您将无法调用任何修改列表的方法(添加、设置、删除、清除等)。

#3


1  

Another alternative approach you may consider apart from other answers which construct a new List<Object> base on your List<Customer>

除了在列表 上构造新列表 的其他答案之外,您还可以考虑另一种方法

Your listCurrentRecords(int objectType) is under your control anyway, if there are no reason of really need to return List<Object>, you may simply change it to public static List<?> listCurrentRecords(int objectType).

无论如何,您的listCurrentRecords(int objectType)在您的控制之下,如果没有必要返回List,您可以将其改为public static List listCurrentRecords(int objectType)。

Actually it is not a very good idea to use wildcard in return type. Let's take one step further, why don't make listCurrentRecords return the proper list instead? For example, instead of passing in an integer, pass in the Class of the type of data you want, e.g.

实际上,在返回类型中使用通配符并不是一个好主意。让我们更进一步,为什么不让listCurrentRecords返回正确的列表呢?例如,与其传递一个整数,不如传递你想要的数据类型的类。

public static <T> List<T> listCurrentRecords(Class<T> dataType)

so the caller can receive a proper list of, for example, Customer by

因此,调用者可以收到适当的列表,例如,Customer by

List<Customer> = listCurrentRecords(Customer.class)

#1


2  

Since you want the Object type (remember generics are a compile time type checking feature), you can use addAll or the constructor that takes a second List. Like

由于需要对象类型(请记住泛型是一个编译时类型检查特性),所以可以使用addAll或获取第二个列表的构造函数。就像

List<Object> myCustomer = new ArrayList<>(getMyCustomer());

or

List<Object> al = new ArrayList<>();
// ...
al.addAll(getMyCustomer());

#2


1  

I would probably do

我可能会做

List<Object> myCustomer = new ArrayList<>(getMyCustomer());

This creates a new list by iterating over the original, so could be slow depending on the size of the list.

这通过遍历原始列表创建一个新的列表,因此根据列表的大小可能会比较慢。

If this is a problem, an alternative is to do

如果这是一个问题,另一种选择就是去做

List<Object> myCustomer = Collections.unmodifiableList(getMyCustomer());

This version works by wrapping the original list. If you do it this way you will not be able to call any methods that modify the list (add, set, remove, clear etc).

这个版本通过封装原始列表来工作。如果您这样做,您将无法调用任何修改列表的方法(添加、设置、删除、清除等)。

#3


1  

Another alternative approach you may consider apart from other answers which construct a new List<Object> base on your List<Customer>

除了在列表 上构造新列表 的其他答案之外,您还可以考虑另一种方法

Your listCurrentRecords(int objectType) is under your control anyway, if there are no reason of really need to return List<Object>, you may simply change it to public static List<?> listCurrentRecords(int objectType).

无论如何,您的listCurrentRecords(int objectType)在您的控制之下,如果没有必要返回List,您可以将其改为public static List listCurrentRecords(int objectType)。

Actually it is not a very good idea to use wildcard in return type. Let's take one step further, why don't make listCurrentRecords return the proper list instead? For example, instead of passing in an integer, pass in the Class of the type of data you want, e.g.

实际上,在返回类型中使用通配符并不是一个好主意。让我们更进一步,为什么不让listCurrentRecords返回正确的列表呢?例如,与其传递一个整数,不如传递你想要的数据类型的类。

public static <T> List<T> listCurrentRecords(Class<T> dataType)

so the caller can receive a proper list of, for example, Customer by

因此,调用者可以收到适当的列表,例如,Customer by

List<Customer> = listCurrentRecords(Customer.class)