Is this an efficient way to search for a specific object in an ArrayList?
Product is the superclasS. Mobo, CPU, RAM, GPU... are the subclasses. ArrayList(called stock) stores Product objects. I consider that they key i am searching is found when class and the attribute "model"(you can see the getter of the attribute in my code) are matching.
public static boolean checkAvailability(ArrayList<Product> stock, Product product) {
boolean found = false;
for(Product p: stock) {
if(p instanceof Mobo && (product.getModel() == p.getModel())) {
found = true;
break;
};
if(p instanceof CPU && (product.getModel() == p.getModel())) {
found = true;
break;
};
if(p instanceof GPU && (product.getModel() == p.getModel())) {
found = true;
break;
};
if(p instanceof RAM && (product.getModel() == p.getModel())) {
found = true;
break;
};
if(p instanceof Monitor && (product.getModel() == p.getModel())) {
found = true;
break;
};
if(p instanceof Keyboard && (product.getModel() == p.getModel())) {
found = true;
break;
};
if(p instanceof Mouse && (product.getModel() == p.getModel())) {
found = true;
break;
};
if(p instanceof Printer && (product.getModel() == p.getModel())) {
found = true;
break;
};
};
return found;
}
getModel()
? \$\endgroup\$ – Dan Apr 11 '18 at 11:10