简体   繁体   中英

How to convert a class object to generic object type?

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.

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,

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,

   // 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.

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

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

or

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

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).

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) .

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, eg

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

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

List<Customer> = listCurrentRecords(Customer.class)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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