Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

Unique "logical" identifier for an object?

Thread view: 
Stuart Leonard - 19 Apr 2004 22:45 GMT
I have a collection of "Invoice" objects within a vector.  I need to provide
a "logical key" to each "Invoice" object, which consists of a vendor number
and an invoice number, which are both Strings.  I need place a logical key
within each "Invoice" object, so that I can retreive the objects later in
the program by this "key".
This seems like a classic problem.  Can anyone provide any ideas of how to
accomplish this?
Stuart Leonard - 19 Apr 2004 22:50 GMT
> I have a collection of "Invoice" objects within a vector.  I need to provide
> a "logical key" to each "Invoice" object, which consists of a vendor number
[quoted text clipped - 3 lines]
> This seems like a classic problem.  Can anyone provide any ideas of how to
> accomplish this?

As  a sidenote, I am using JDK 1.1.8, which only has three types of
containers (array, vector, and hashtable).
I realize that this is an old version, but for the time, I must stay here.
Bryce (Work) - 19 Apr 2004 22:53 GMT
>I have a collection of "Invoice" objects within a vector.  I need to provide
>a "logical key" to each "Invoice" object, which consists of a vendor number
[quoted text clipped - 3 lines]
>This seems like a classic problem.  Can anyone provide any ideas of how to
>accomplish this?

see hashcode.

myObject.hashCode();

--
now with more cowbell
Roedy Green - 22 Apr 2004 08:31 GMT
>I have a collection of "Invoice" objects within a vector.  I need to provide
>a "logical key" to each "Invoice" object, which consists of a vendor number
[quoted text clipped - 3 lines]
>This seems like a classic problem.  Can anyone provide any ideas of how to
>accomplish this?

Collections like Objects as keys.  You could use Strings or Integers
or Longs.

Your objects can have three keys.  Vendor, invoice, and
 vendor * 10000000 + invoice.

Your Set methods for vendor and invoice and autobuild the third key.

Now you has have three collections -- to seek by vendor, invoice or
vendor/invoice.

This is assuming invoice numbers are globally unique, rather than
unique within vendor.  If not you can't access by invoice number
because Collections demand unique keys.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Steve Horsley - 22 Apr 2004 20:31 GMT
> I have a collection of "Invoice" objects within a vector.  I need to provide
> a "logical key" to each "Invoice" object, which consists of a vendor number
[quoted text clipped - 3 lines]
> This seems like a classic problem.  Can anyone provide any ideas of how to
> accomplish this?

I think I might be inclined to be lazy and make a key like so...

   myHashtable.put(vendorNumber + "|" + invoiceNumber, invoice);

Short and dirty. It might be suitable for you.

Steve
Roedy Green - 22 Apr 2004 23:12 GMT
>I think I might be inclined to be lazy and make a key like so...
>
>    myHashtable.put(vendorNumber + "|" + invoiceNumber, invoice);

I wrote a little essay http://mindprod.com/jgloss/binaphobia.html

There is a tendency to use giant fluffy strings for all data.

This is not good when the underlying data are actually integers
because

1. Strings don't collate correctly unless you arrange for lead zeroes.

2. Strings are bulky.  +9,223,372,036,854,775,807 as a long takes 8
bytes.  As a String, it takes 38 bytes plus perhaps another 16 bytes
of object overhead.

3. there is no guarantee all the characters are numeric.

4. Strings take much longer to compare, basically they compare
character by character. Longs compare all at once.

5. You can't do arithmetic on Strings

Bottom line: use int, long, Integer, Long in preference to String
espcially for in-RAM data.  Making them strings in datafiles can help
debugging, but even then, you can flip to compact binary format for
production.

.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.