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 / November 2005

Tip: Looking for answers? Try searching our database.

HashMap with primitive int key

Thread view: 
Andrew E - 28 Nov 2005 20:45 GMT
Dear All,

I have a class like this:

    class CustomerOrder {
        int globalID;
        ...
    }

I have an array of these.
I'd like to store an index of globalIDs, so I can do:

    CustomerOrder o = index.get(123)

I could modify my class to be:

    class CustomerOrder {
        Integer globalID;
        ...
    }

Perhaps it is my C++ background, but the idea of allocating an object for every
ID when I can just use an int bothers me.

I figure a HashMap is the right tool of choice, e.g.:

    HashMap<Integer, CustomerOrder> index = new ...

Has anyone out there already written a modified hashmap class that uses int as
the key?

Thanks for any tips :)

Andrew
Thomas Hawtin - 28 Nov 2005 21:18 GMT
>     CustomerOrder o = index.get(123)

> Perhaps it is my C++ background, but the idea of allocating an object for every
> ID when I can just use an int bothers me.

The overhead for allocating a small object is actually very small. It
shouldn't come as a surprise that Java runtimes are heavily optimised
for allocating small, short-lived objects. I get around 30-40 cycles on
my PII. Can be as low as 10 instructions on some architectures.

You've actually picked a number in an interesting range. Integer.valueOf
(which autoboxing uses) always returns the same value for at least those
integers in [-128, 127]. Years ago when I was dealing with an applet
which generated lots of small Integers, I used an array of the first 256
to deal with the almost always small IDs. If nothing else it cleared
some mess off the profiler.

If you are dealing with large numbers but often repeating the same
values there is a cunning thread-safe trick that works from 1.5:

http://jroller.com/page/tackline?entry=fast_immutables

(Note comments.)

> Has anyone out there already written a modified hashmap class that uses int as
> the key?

IIRC there is a Apache Jakarta Commons Primitives subsubsubproject for
primitive collections. But I suggest that you first visit Mr OptimizeIT,
Mr JProbe or one of their friends before adding dependencies and
obscurities. ;)

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



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.