File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes HashMap - can a value contain executable code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashMap - can a value contain executable code?" Watch "HashMap - can a value contain executable code?" New topic

HashMap - can a value contain executable code?

Derrick Williams

Joined: Nov 27, 2010
Posts: 9

Hi All-

I've run into a curious problem on an application I'm working on, and thought I'd throw it out to all the smart, attractive, and fantastic people here at JavaRanch, and that especially includes *you*! I would love to hear how you would code this problem, as I'm thinking there has to be a better way.

The application itself is rather ordinary, nothing terrifically special, it reads a stream of data to process here in Whizzbang Widgets International, filling orders from happy customers worldwide. The (sole) interesting point is that the data includes small text tags, which tell the application to perform a small task. When my application reads these tags, it performs a simple action, like change a setting, call a function, write a message to the log, none of these tasks are any more than 4 lines of code at the very most. However, there are a few thousand of these text tags, so when one comes in, I've got a giant function that looks like this:

alertSalesDude("Order placed for WhifferBang Gadget!");
}else if(textTag.equals("Florpzwibble")){
changeZlrpwitz("We're out of Gozoinkz! Order some more!");
}else if(textTag.equals("Zowzums")){
writeAlert("Discontinued item! Remove from catalog!");
alertSalesDude("Zowzums ordered, call customer!");
}else if....................

The above code can't possibly be efficient, especially when new tags are being added and the volume of tags are increasing, thanks to the end of the recession and more customers ordering more products. Maybe if I'm lucky the tag's found near the very top, but it could very well be at the very bottom. No telling what tag could roll in at any time.

I was thinking it would be awesome if I could use a HashMap or Hashtable, and have the tag as a key, but make the value contain the executable code. It would be a total win, the hashmap would return my object lickety-split, and I could execute the object to run my few lines of code. I don't really know how to do that, code can be grouped between { } brackets, but that doesn't make it an object.

Can this be done? I could make a class for the object, but would it mean instead of one giant file, I'd have thousands of classfiles?

I'm not a real super expert in Java, but at any way I'm ready to move away from my giant frankenstein "if/else if" monstrosity!

Thanks very much-


Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33132

Welcome to JavaRanch/CodeRanch!

No, a value cannot contain executable code. It can contain a Java object though. One thing you could do if you have a fairly small set of things that need to be done is write your own object that can execute different types of things. Like alerts, logs, etc. Kind of like commands. Then you have a limited number of subclasses.

This approach is going to involve understanding the commonalities you do have.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
pete stein

Joined: Feb 23, 2007
Posts: 1561
I wonder if the Command Pattern would be useful here, where the value held in the HashMap is say a Runnable (or some other interface if necessary) object that holds the code of interest.


* note code not tested

edit: I think this is somewhat a subset of what Jeanne posted above.
Derrick Williams

Joined: Nov 27, 2010
Posts: 9
Hi Guys-

Your suggestions were spot on! It was a huge slog grinding through the giant if/else list, but I took your suggestions and examples and made a simple interface like this:

public interface ReturnTagValue {

String findVal(WidgetDataSource widgetInfo);


Where WidgetDataSource was a class specific to my application necessary to perform its tasks.

Then I declared a HashMap that looked like this:

private static HashMap<String,ReturnTagValue> commandMap = null;

Then my instantiator for my TagValues class that processed the tags looked like this:

public TagValues()
if(commandMap==null){"Generating new command map for TagValues");

commandMap = new HashMap<String,ReturnTagValue>();

commandMap.put("{Glorpzwobble}",new ReturnTagValue(){
public void findVal(WidgetDataSource widgetInfo){
AlertSalesDude(widgetInfo.assignedSalesDude(),"Customer ordered a Glorpzwobble!"):

Starting up this class takes a little bit of time, but I only have to do it once. The code that executes is reduced to this line (minus error checking, error recovery, etc):


Now the tag lookup code runs like a dream! Not only is the lookup visibly faster, it also is a lot smoother. The previous version had a characteristic herky-jerky response, as tags could have been found in the very beginning of the old if/else block, or more toward the end. I wish I'd had a presence of mind to capture some timing data to see exactly how much of a speedup I got. It probably wouldn't have been much use to anyone else though, as there's a lot of other stuff going on with the application that would cloud the numbers, but it's safe to say the speedup is 'a lot'!

Anyway thanks again, truly appreciate it!


I agree. Here's the link:
subject: HashMap - can a value contain executable code?
It's not a secret anymore!