I was wondering something. If I have a class like this:
public class OrderItem {
private Item item;
private int quantity;
private BigDecimal total;
}
And I have in the class Chart a List<OrderItem> orders. OrderItem is identified by its item so I was thinking if there is a method of orders I can ovveride (maybe indexOf) to check if an OrderItem exists and then retrieve it (or its index)
I’m talking about something different from using a for loop and check its item. Maybe an interface?
edit: I’m sorry I mislead the question, I forgot an important part. I need to retrieve the object.
Over-ride
.equals()and.hashcode()inOrderItemso equality is determined byitem.You can then call
indexOf(dummyOrderItem), where dummyOrderItem is a dummy object created with the correctitem. You can then call get(index) to retrieve the true object.If you use Eclipse, you can generate these methods automatically using
source->generate hashCode() and equals():