manaswi31 manaswi31 - 3 months ago 4
Java Question

Loop through List<object> to determine if particular object type exists java

In Equip.java, I have a list getAorBorCorD as follows:

class Equip{
protected List<Object> getAorBorCorD;

class A{
}
class B{
}
class C{
}
class D{
}
}


In another module,

Equip equip = new Equip();
equip.getAorBorCorD().?????;


How can I check if getAorBorCorD contains Object type A ?

Answer

Just loop through your list

for(Object object: objects){
    if(object instanceof A){
       // Do something wiht it
    }
}
Comments