aspose file tools*
The moose likes Java in General and the fly likes Using enums as a Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using enums as a "light weight" hash table" Watch "Using enums as a "light weight" hash table" New topic
Author

Using enums as a "light weight" hash table

Phil Walterson
Greenhorn

Joined: Oct 08, 2004
Posts: 7
I wanted to discuss efficiency issues with the above.

Here's some background info:

Recently I started using the enum type and liked its capabilities. The example below is based on simple Role Playing Game concepts:

// constants defining various types of abilities, good for readability.
public static enum Abilities {strength, dexterity, constitution, wisdom, intelligence, charisma};

// Then you have an array that stores their values
public int[] abilities = new int[6];

// From that point on, whenever you want to get/set the scores you can
// do something like:
abilities[Abilities.strength.ordinal()] = 12;
abilities[Abilities.intelligence.ordinal()] = 13;

Here you're using the constant enums as a kind of key to get to the correct position in the array, as each ability constant has a unique position in the enum list.

My question is:

Is it performing an O(n) search operation over the list of constants every time you do Abilities.strength.ordinal()? Or does each element know it's ordinal and quickly return it. That is, is it looking up a constant's position by iterating through the enum list everytime ordinal() is called? If so it might not be such an efficient idea.

I was trying to implement a small structure to be easily readable and have constant time access like a Hashtable. I'm not using a Hashtable because I worry that each Hashtable instantiation comes with some memory overhead (I could be wrong about this) and using them all over the place might not be desirable. But since I don't know the inner workings of enum this may or may not be better.
[ December 26, 2004: Message edited by: Philwx ]
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
don't worry about the memory overhead.
Of course Hashing algorithms cause extra memory use, that's the price for higher performance.

And don't use Hashtable, use HashMap instead. It is a lot more CPU friendly.


42
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
You are optimizing way too early. The key is to first create a design that is usable, expressive and simple to code. Only once it's working and you've benchmarked it and found that it isn't fast enough should you redesign it. This kind of overhead, unless it's part of a MMORPG, will go totally unnoticed by you.

I haven't used enums yet. Do they sart at 1 or 0 (or do you specify the values for each?). If they start at 1, you'd need to make your arrays one larger than usual or subtract 1 each time you access it. Regardless of what you decide to do, put the structure behind a class that gives you encapsulation.

Here's a start.This allows generic access using the enums (for looping to get all scores) and specific accessors which are nicer to read in code, especially if you implement the game rules in code.

Given the above class, if you later decide to use a HashMap to store the scores, no code using the AbilityScores class has to change as a result -- just the internal implementation of the class itself.
Phil Walterson
Greenhorn

Joined: Oct 08, 2004
Posts: 7
Thanks for the info, I think you're both right that I'm thinking efficiency too quickly, and that's not proper design methodology.

I have a complete class for it already, btw. I guess I didn't post it because I'm new to the open source community and didn't know if I should go around posting code to forums. But it is too early to determine efficiency without other classes which haven't been made yet. Since a lot of the work I will be doing is redundant, I figured I would cheat and try to make a decision on whether to use this idea right away. Bad me

Incidentally, the methods you posted are an exact subset of what mine look like, except I don't have specific accessors (which is ok).

And yep the enum ordinals do start with 0, choosing 6 was deliberate.

Thanks for the guidance.

Phil
[ December 26, 2004: Message edited by: Philwx ]
kri shan
Ranch Hand

Joined: Apr 08, 2004
Posts: 1373
HI Jeroen,
How HashMap is CPU friendly ??
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Maps minimise lookup time (if you have an effective hashing mechanism). That's what they're there for...

Less lookup time means less CPU cycles needed for the lookup.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
One other (probably obvious) thought about using indexes into an array - with enums or not - is that they are only guaranteed consistent for one run of the program. That is, you might insert a new enum in the next build and change the value for "wisdom" from 4 to 5. That might be perfectly ok as long as all values are loaded and read in the same run. But if you persisted the array to database or file on Monday and read it on Tuesday and the indeces had changed, you'd have a mess. If you persist a map with key value pairs you'll always be able to retrieve "wisdom".


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

"Philwx"-
Welcome to the JavaRanch! We like to keep a professional lookin' image around here, and don't really fancy it when people try to show up the one-eyed moose. So if you could change your display name (click here), that'd be just dandy. (Basically... it should be a legit first and last name. If you'd like a more thorough reason as to why I'm asking you to change your name, check out the Naming Policy.)

Thanks! And again, Welcome to the JavaRanch!
Phil Walterson
Greenhorn

Joined: Oct 08, 2004
Posts: 7
Done.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Thanks Phil! Much appreciated.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Jeroen Wenting:
And don't use Hashtable, use HashMap instead. It is a lot more CPU friendly.

Originally posted by kri shan:
How HashMap is CPU friendly ??
The old Hashtable class is synchronized, yet most are used by a single thread only. Thus, the new Collections classes are not syncrhonized themselves, providing better performance. If you need thread-safety, you can wrap them using Collections.synchronizedList/Map/Set() or the even newer ConcurrentHashMap.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Using enums as a "light weight" hash table