Instanceof with List in JAVA [closed]

-1

I declare a method that gets a List of type Object . However, when I try to recognize it with instanceof it gives error. I tried to do a casting but it still gave an error and I could not identify where the problem is in my code. Can anyone help?

public static void insertLog(List<Object> list) throws IOException{

    if (list instanceof List<Transaction>){
        for(Transaction transaction : list){
            insertLog(transaction.toString());
        }
    }

    else if (list instanceof List<Data>){
        for(Data data : list){
            insertLog(data.toString());
        }
    }

}
public static void insertLog(CashTransactionRequest request) throws IOException{
    insertLog(request.getClient().getCustomer());
    insertLog(request.getClient().getTeller());
    insertLog(request.getClient().getTellerName());

    insertLog(request.getDevice().getCountryId());
    insertLog(request.getDevice().getDelegation());
    insertLog(request.getDevice().getDeviceId());
    insertLog(request.getDevice().getDeviceName());
    insertLog(request.getDevice().getDeviceType());
    insertLog(request.getDevice().getTimeZone());

    insertLog(request.getAdditionalData().getData());

    insertLog(request.getTransaction());
}
    
asked by anonymous 18.09.2017 / 14:44

3 answers

4

You can check the item instead of the list:

public static void insertLog(List list) {
  for (Object item : list) {
    if (item instanceof Transaction) {
      insertLog((Transaction) item);
    } else if (item instanceof Data) {
      insertLog((Data) item);
    }
  }
}

I noticed that in your example you use String . To do toString is not necessary to perform conversion, then it is not necessary to perform verification either.

    
18.09.2017 / 15:16
3

List<Transaction> is not a List<Object> . List<Data> is also not List<Object> . The reason is that in List<Object> you can add any object, whereas in List<Transaction> you can only add objects of type Transaction . In List<Data> you can only add Data objects.

Every object has a getClass() method that returns another object (of type Class ) that represents the class of the object. If you do this:

List<Transaction> lista1 = new ArrayList<Transaction>();
System.out.println(lista1.getClass().getName());
List<Data> lista2 = new ArrayList<Data>();
System.out.println(lista2.getClass().getName());
System.out.println(lista1.getClass() == lista2.getClass());

It will print:

java.util.ArrayList
java.util.ArrayList
true

That is, both lists have the same type. The generic does not matter.

The instanceof operator looks at the getClass() class. The generic is lost in this case. That is, it's no use doing instanceof List<Transaction> or instanceof List<Data> , since instanceof does not see generics and only sees List .

Why do not you just do that?

public static void insertLog(List<Object> list) throws IOException {
    for (Object item : list) {
        insertLog(item.toString());
    }
}
    
18.09.2017 / 15:28
1

In the @Sorack response , it provides two alternatives for how to handle the issue. The @VictorStafusa response provides more detail and offers the solution via toString() mentioned by @Sack. I'm going to propose something more functional here.

  

PS: a little more focused because it could be treated very well in a more idiomatic way, but since I'm away from a computer I can not validate

Suppose you know how to enter all information in the log . Can be E for objects of type Logger.insertData and Data for objects of type Logger.insertTransaction .

public static <E> void insertLog(List<E> list, Consumer<E> logAtom) throws IOException {
    for (E atom : list) {
        logAtom.apply(atom);
    }
}


// exemplo de chamada
public static void insertLog() {
  List<Transaction> transacoes = getTransacoes();
   insertLog(transacoes, Logger::insertTransaction);

  List<Data> datas = getDatas();
  insertLog(datas, Logger::insertData);
}
    
18.09.2017 / 15:32