File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes HashMap with umm...method invocations? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HashMap with umm...method invocations?" Watch "HashMap with umm...method invocations?" New topic

HashMap with umm...method invocations?

Alex Birmingham
Ranch Hand

Joined: May 22, 2008
Posts: 54
Hey all,

Here is my requirements list for a certain class:

1. Needs to loop endlessly, at the beginning of each loop ask for player input.

2. Needs to invoke methods based on that player input.

3. Needs to recognize commands even if player only partially types out the word. For example, 'e' instead of 'east.'

So my thought is that I need to create a collection of strings which are linked to method invocations. I feel that the collection part is important, because then I can accomplish # 3 with: if(string.contains("e")). Additionally, I could have a player command which prints the collection, thus informing a player of all potential commands.

The most efficient way of doing this that I can think of is just initializing all the strings at runtime, then adding them to an ArrayList<String>, then doing a long if-else-if statement on the contents of the ArrayList<String> vs. the player command.

But it seems like there should be a better way... For example, is there any way to manuever HashMap into storing method invocations as values? That way I 'key' strings and then map corresponding methods as values. That would cut the if-else-if statement down a TON, and it would be far more versatile in adding new commands.

Any thoughts?

- SCJA 1.0
Alex Birmingham
Ranch Hand

Joined: May 22, 2008
Posts: 54
My apologies, I should have just tried to do it. It's entirely possible, with no tricks whatsoever. Very cool.

Example, just in case anyone is curious:

PS - I think I'm addicted to HashMaps. =D
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63866

Originally posted by Alex Birmingham:
hm.get("east"); // invokes grid.transport(player, "east"));

I'm afraid that's not the way that it works.

The transport method was invoked during your put operation, and the result of the method was stored as the Map entry value.

Your get call, because you aren't storing the result anywhere, performs no significant operation.
[ June 17, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [About Bear] [Books by Bear]
Alex Birmingham
Ranch Hand

Joined: May 22, 2008
Posts: 54
Ouch, good point. =(

Well, I guess I'm back to square one then. Any thoughts on a more efficient manner than an ArrayList<String> and corresponding if/else-if statements?
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375

In java, instead of thinking method invocations, I think callback routines/functions which simplifies the understanding for me

You could possibly do the following

It's essentially a method invocation, but not really

Ashish Hareet
Alex Birmingham
Ranch Hand

Joined: May 22, 2008
Posts: 54
Thanks a lot Ashish! I'll sit down and test this out a little bit later, but this sounds like a good place to start.

As a side note, I definitely haven't rounded out my interface/inheritance/polymorphism toolkit to the extent that I could/should. After learning that you can just handle variability amongst instances of a class with a HashMap, and just add whatever uniqueness to a class you want by introducing new values, it seems like this would be the natural tendency.

I guess I still need to round out that other toolkit though. =)
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
By the way, Ashish's solution makes use of the Command design pattern.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Uncle Ed Hatfield

Joined: May 24, 2008
Posts: 10

I'm trying to understand hashmaps too...

Does Ashish's answer mean that Alex could then double up commands
that do the same thing (like 'e' and 'east') with:

map.put("e", map.get("east"));

So there'd be the original definition of what 'east' does, and then
we set 'e' to do the same thing. Yes?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46397
Originally posted by Uncle Ed Hatfield:
map.put("e", map.get("east"));
It would have to be a Map<String, String>. Try it and see. I think it would work, yes.

[edit]No, it wouldn't have to be a Map<String, String> Sorry, I was mistaken. It would be a Map<String, SomethingElse>[/edit]
[ June 18, 2008: Message edited by: Campbell Ritchie ]
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

yes yes yes command design pattern.

Will rid you of all the if else clutter.

Just read the pattern a few days back.


I agree. Here's the link:
subject: HashMap with umm...method invocations?
It's not a secret anymore!