gauti
gauti

Reputation: 557

Find Object type in empty list

consider the below situation. How to find the empty list takes which object type.

List<User> userList = new ArrayList();
List<Address> addList = new ArrayList();
method1(userList);
method1(addList);
void method1(List<?> list){
//Now list is empty;
//how to find list accepts User type object or Address type object
    }

Upvotes: 2

Views: 1851

Answers (2)

RCS
RCS

Reputation: 1432

There no way you can get the object type from an empty list if it has wildcard in it. If you are passing empty list and trying to access the list then NullPointerException will come. If you are passing empty list and not using the list then it will run. Wildcard is used for unknown type. If you are using list with wildcard then type will be decided based on the object it contains. If list is empty that means you can't have any information about the object type it belongs to. Here is an example, where i am passing list which contains the element and i am able to get the object type of the list it belongs to. You can use something like this to get the object type. If you can't then please explain your use case that will help to help you out. Thanks.

public class GenericsTest 
{
        List<User> userList = new ArrayList();
        User aa1 = new User(20, "Mine");
        User aa2 = new User(10, "Yours");
        userList.add(aa1);
        userList.add(aa2);
        List<Address> addList = new ArrayList();
        Address bb1 = new Address("20", "A B Road", "Kolkata");
        Address bb2 = new Address("10", "B C Roy Road", "KOlkata");

        addList.add(bb1);
        addList.add(bb2);

        method1(userList);
        method1(addList);
        method2(userList);
        method2(addList);
        public static void method1(List<?> list)
        {
            if(list.get(0) instanceof User)
            {
                System.out.println("I am User");
            }
            if(list.get(0) instanceof Address)
            {
                System.out.println("I am Address");
            }
        }
        public static void method2(List<?> list)
        {
            System.out.println("If you are not using list and have your own custom logic to find the type");
        }
}
class User
{

    int a1;
    String b1;
    public User(int a1, String b1) {

        this.a1 = a1;
        this.b1 = b1;
    }

}
class Address
{
    String line1;
    String line2;
    String line3;

    public Address(String line1, String line2, String line3) 
    {

        this.line1 = line1;
        this.line2 = line2;
        this.line3 = line3;
    }   
}

Upvotes: 0

Troncador
Troncador

Reputation: 3536

It is impossible. Java erases the type after the compilation.

But you can do something like this:

<T> void method1(List<T> list, Class<T> tclass){
  // you force the class T as argument
}

Calling to "method1" is a little different.

List<User> userList = new ArrayList();
method1(userList, User.class);

Upvotes: 1

Related Questions