-2
I have declared a method that receives a List
of the kind Object
. But when I try to recognize him as instanceof
it gives error. I have tried to do a casting but still giving error and could not identify where is the problem in my code. Someone can 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());
}
You can tell what mistake it is ?
– Dev
In Java, the Generics reference is not stored. So, Java cannot tell the difference between
List<Banana>
ofList<Laranja>
. So you might end up mixing bananas with oranges if you’re not careful– Jefferson Quesado
Can add doubt code?
– user28595
Also, prefer to publish code to the image. I would even try to make a correction to what you wrote, but as it is in image there is no copy and paste. Even, the fact of having used images and not codes may be the reason for the negative vote
– Jefferson Quesado
If you’re gonna use the
toString
there’s no point in casting– Sorack
Check this out: https://docs.oracle.com/javase/tutorial/java/generics/erasure.html
– Victor Stafusa