Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap with umm...method invocations?

 
Alex Birmingham
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Alex Birmingham
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 64827
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Alex Birmingham
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex,

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

HTH
Ashish Hareet
 
Alex Birmingham
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, Ashish's solution makes use of the Command design pattern.
 
Uncle Ed Hatfield
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm,

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
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes yes yes command design pattern.

Will rid you of all the if else clutter.

Just read the pattern a few days back.

Priety.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic