Become a part of the TranceAddict community!Frequently Asked Questions - Please read this if you haven'tSearch the forums
TranceAddict Forums > Main Forums > Chill Out Room > Java Programming Question...need some help
  Last Thread   Next Thread
Share
Author
Thread    Post A Reply
Vaedyn
Junior tranceaddict



Registered: Sep 2003
Location: Southbridge, Ma
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

Old Post Jan-04-2005 21:00  United States
Click Here to See the Profile for Vaedyn Click here to Send Vaedyn a Private Message Add Vaedyn to your buddy list Report this Post Reply w/Quote Edit/Delete Message
cviper
Lurking on TA



Registered: Sep 2001
Location: NGC-6543 TekniskFysik - levande lik!

If you are doing a single test (as you mentioned in your post), I'd go for the equals method.

In equals() you can could test if the Object passed to it is of type Product or ProductID:

// Somewhere in you Product class
public boolean equals(Object obj)
{
if( obj.getClass() == ProductID.class )
{ compare with the ProductID }
else if( obj.getClass() == Product.class )
{ compare the "whole" Product Object, using getHash() or something }
else return false;
}

That way you should not break anything else You probably also need to overload the equals method for the ProductID class...

If you're going to compare more than a few objects, a HashMap probably would speed up things.


___________________
The fears of the past are the dreams of the future

Old Post Jan-05-2005 00:42  Sweden
Click Here to See the Profile for cviper Click here to Send cviper a Private Message Add cviper to your buddy list Report this Post Reply w/Quote Edit/Delete Message
Operation-Green
tranceaddict in training



Registered: Aug 2004
Location: Oregon

Just out of curosity is this for the web or a program?
I dont know much about Java but what I do know is I hate working with it, I orginally did my website in mostly java, and it dint work with a lot of browsers.


___________________

Old Post Jan-05-2005 00:58  United States
Click Here to See the Profile for Operation-Green Click here to Send Operation-Green a Private Message Visit Operation-Green's homepage! Add Operation-Green to your buddy list Report this Post Reply w/Quote Edit/Delete Message
smokeape
Lowland Trance Addict



Registered: Jul 2003
Location: Heart of Dixie

Sorry, Visual Basic dude here with database programming. I can follow the programming logic you listed to a degree, but cannot give you any advice worth a damn. Step in my world and I'm an absolute pro though.


[[[smoke]]]

Morgan Page & Gregory Shiff ft Astrid Suryanto - All I Know (Cattaneo & Garcia Mix)

Old Post Jan-05-2005 01:03 
Click Here to See the Profile for smokeape Click here to Send smokeape a Private Message Add smokeape to your buddy list Report this Post Reply w/Quote Edit/Delete Message
Vaedyn
Junior tranceaddict



Registered: Sep 2003
Location: Southbridge, Ma

Thanks all. I went the HashMap route.


Thanks Again,

Vaedyn

Old Post Jan-06-2005 04:10  United States
Click Here to See the Profile for Vaedyn Click here to Send Vaedyn a Private Message Add Vaedyn to your buddy list Report this Post Reply w/Quote Edit/Delete Message

TranceAddict Forums > Main Forums > Chill Out Room > Java Programming Question...need some help
Post New Thread    Post A Reply

 
Last Thread   Next Thread
Click here to listen to the sample!Pause playbackTrack 5 From Hydroid - Deepwater Sessions 003 07-18-05 [2006] [2]

Click here to listen to the sample!Pause playbackDenzil & Dwayne - Force of Habit [2002]

Show Printable Version | Subscribe to this Thread
Forum Jump:

All times are GMT. The time now is 13:08.

Forum Rules:
You may not post new threads
You may not post replies
You may not edit your posts
HTML code is ON
vB code is ON
[IMG] code is ON
 
Search this Thread:

 
Contact Us - return to tranceaddict

Powered by: Trance Music & vBulletin Forums
Copyright ©2000-2026, Jelsoft Enterprises Ltd.
Privacy Statement / DMCA
Support TA!