Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Instances of ArrayList using reflection

Tags:

java

I have a method which accepts an Object. The object is a list. The list could be any type. I would like to identify the type of list using reflection and create an instance of the list & iterate over it and print the results.

Different Types of List

List<Customer> customerList = new ArrayList<Customer>();
List<Service> serviceList = new ArrayList<Service>();
List<Product> productList = new ArrayList<Product>();

Method that accepts the list

public void printList(Object genericList){

// identify the type of list
// create the list object
// iterate over the list and print the values

}
like image 802
Punter Vicky Avatar asked Dec 02 '25 07:12

Punter Vicky


2 Answers

Because of type erasure, there's no way to tell the generic type of a collection at runtime (which incidentally makes Object genericList an oxymoron). But if you just want to print them out regardless of the type, you can just cast it to List<?>:

public void printList(Object obj){
    for (Object o : (List<?>)obj) {
        System.out.println(o.toString());
    }
}

If you need a more type-specific implementation, and you know the lists won't be empty, and you're confident that there's no overlap between list types (i.e. customerList cannot contain Products and vice-versa), then you can technically do something like this:

public void printList(Object obj){
    List<?> list = (List<?>)obj;
    Object firstElement = list.get(0);
    if (firstElement instanceof Customer) {
        printCustomers((List<Customer>)list);
    } else if (firstElement instanceof Product) {
        printProducts((List<Product>)list);
    } //...
}

Note that the compiler will complain about these casts, and for good reason. It has no way of knowing whether you're telling the truth about the generic type, at least not until you try reading a value from the list, at which point you'll get a ClassCastException if you've made a mistake.

like image 120
shmosel Avatar answered Dec 04 '25 19:12

shmosel


If you want to judge the data type of the object from a list and print out the value, I would like to say that judging might not need the reflection since instanceof would satisfy your need. To print out the value or to have more manipulations on the specific object you could use the java reflection. It's very fragile. Currently I just list the codes of a simple model. If you confirm this is what you need then we could discuss more.

public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException{

    List<Customer> customerList = new ArrayList<>();
    List<Service> serviceList = new ArrayList<>();
    List<Product> productList = new ArrayList<>();
    List result = returnFunc();
    printList(result);

}


public static void printList(List genericList) throws ClassNotFoundException, InstantiationException, IllegalAccessException{
    Object obj=null;
    String type = "";
    Class c1 = null;
    // identify the type of list
    if(genericList.get(0) instanceof Customer){
        c1 = Class.forName("Customer");
    }else if(genericList.get(0) instanceof Service){
        c1 = Class.forName("Service");
    }else if(genericList.get(0) instanceof Product){
        c1 = Class.forName("Product");
    }else{
        System.out.println("Error");
    }
    // create the list object
    Object instance = c1.newInstance();
    // iterate over the list and print the values
    for(Object o : genericList){
        System.out.println(o.toString());

    }
}
//to randomly output a list
public static List returnFunc(){
    ArrayList<List> tmp = new ArrayList<>();
    ArrayList<Object> list_customer = new ArrayList<>();
    list_customer.add(new Customer());
    ArrayList<Object> list_service = new ArrayList<>();
    list_service.add(new Service());
    ArrayList<Object> list_product = new ArrayList<>();
    list_product.add(new Product());
    tmp.add(list_customer);
    tmp.add(list_service);
    tmp.add(list_product);
    Random r = new Random();
    return tmp.get(r.nextInt(3));
}
like image 24
Shawn. L Avatar answered Dec 04 '25 19:12

Shawn. L