Better than HashMap

I feel like i'm doing something wrong when I'm using HashMap to map:int --> Objectseems like a waste that the int is being turned into an Integer, making the operation more complex than it has to be.What would be a better solution?Thanks!
[278 byte] By [Nethera] at [2007-11-27 5:26:59]
# 1
If the ints will never be negative, you can use an array.But you shouldn't worry about object creation of wrapper types, unless you've run your app through a profiler and know for a fact that it's causing problems.
paulcwa at 2007-7-12 14:48:14 > top of Java-index,Java Essentials,Java Programming...
# 2

> seems like a waste that the int is being turned into an Integer,

Don't worry about it.

[url=http://java.sun.com/developer/technicalArticles/Interviews/goetz_qa.h tml]Writing Better Code: A Conversation With Sun Microsystems Technology Evangelist Brian Goetz[/url]

Q: So how can developers write Java code that performs well?

A: The answer may seem counterintuitive. Often, the way to write fast code in Java applications is to write dumb code -- code that is straightforward, clean, and follows the most obvious object-oriented principles. This has to do with the nature of dynamic compilers, which are big pattern-matching engines. Because compilers are written by humans who have schedules and time budgets, the compiler developers focus their efforts on the most common code patterns, because that's where they get the most leverage. So if you write code using straightforward object-oriented principles, you'll get better compiler optimization than if you write gnarly, hacked-up, bit-banging code that looks really clever but that the compiler can't optimize effectively.

So clean, dumb code often runs faster than really clever code, contrary to what developing in C might have taught us. In C, clever source code turns into the expected idiom at the machine-code level, but it doesn't work that way in Java applications. I'm not saying that the Java compiler is too dumb to translate clever code into the appropriate machine code. It actually optimizes Java code more effectively than does C.

My advice is this: Write simple straightforward code and then, if the performance is still not "good enough", optimize. But implicit in the concept of "good enough" is that you need to have clear performance metrics. Without them, you'll never know when you're done optimizing. You'll also need a realistic, repeatable, testing program in place to determine if you're meeting your metrics. Once you can test the performance of your program under actual operating conditions, then it's OK to start tweaking, because you'll know if your tweaks are helping or not. But assuming "Oh, gee, I think if I change this, it will go faster" is usually counterproductive in Java programming.

Because Java code is dynamically compiled, realistic testing conditions are crucial. If you take a class out of context, it will be compiled differently than it will in your application, which means performance must be measured under realistic conditions. So performance metrics should be tied to indices that have business value -- transactions per second, mean service time, worst-case latency -- factors that your customers will perceive. Focusing on performance characteristics at the micro level is often misleading and difficult to test, because it's hard to make a realistic test case for some small bit of code that you've taken out of context.

~

yawmarka at 2007-7-12 14:48:14 > top of Java-index,Java Essentials,Java Programming...
# 3
I remember reading that when it first appeared on java.sun.com. I actually printed it out and tacked it up at my cubicle. It was one of the best articles I have read in a very long time. 10 points to Goetz for that one.
Navy_Codera at 2007-7-12 14:48:14 > top of Java-index,Java Essentials,Java Programming...
# 4
What are these ints? Can you store them as a member of your Objects? Then you could store your objects in a Collection and do a search for the number when you need to retrieve an object. Not sure if this would be more or less efficient.
floundera at 2007-7-12 14:48:14 > top of Java-index,Java Essentials,Java Programming...