|
Java Programming Question...need some help
Alright programming gurus...I am in need of some help. Here is the current situation.
If I have a Collection of Products and I want to test if the collection contains a given product using ProductID to look it up, how should I handle it?
Assuming Product.equals() tests for the equality of the Product.productID property, I could create a temporary Product object with the right ProductID and say:
Product testProduct = new Product();
testProduct.setProductID(new ProductID(targetProductID));
if( collection.contains( testProduct ) )
{
// found it....
}
But that seems wasteful. I'd really like to be able to do:
if( collection.contains( targetProductID ) )
But that requires the Product object to consider ProductID objects to be equivalent as long as the ID matches, which seems to break the semantics of equals().
I could build a HashMap from the Collection, keyed by ProductID, but that's a lot of work if I only want to perform one test.
How should I handle this?
I was leaning more towards the HashMap. Let me know what you all think.
Thanks in advance and sorry for the long post
Vaedyn
|