Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default Hashmap to Methods

    I have an if-else ladder where
    user inputs 1 - call book()
    user inputs 2 - call movie()
    user inputs 3 - call user()

    If i want to remove the if-else ladder and implement a hash map to the methods, how is it possible.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    A Map holds a Key/Value pair. Here the Key could be an Integer, and the Value could be an object that implements an interface, perhaps Runnable would work well for you, that calls the desired method.

  3. #3
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default

    can you get any links or code fragment for the same

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Java Code:
    import java.util.HashMap;
    
    public class Foo {
    
       public static void main(String[] args) {
          HashMap<Integer, Runnable> myMap = new HashMap<Integer, Runnable>();
          
          myMap.put(1, new Runnable() {
             public void run() {
                book();
             }
          });
          myMap.put(2, new Runnable() {
             public void run() {
                movie();
             }
          });
          myMap.put(3, new Runnable() {
             public void run() {
                user();
             }
          });
          
          for (int i = 1; i < 4; i++) {
             System.out.print("myMap.get(" + i + ").run() outputs: ");
             myMap.get(i).run();
          }
          
       }
       
       public static void book() {
          System.out.println("Book");
       }
       
       public static void movie() {
          System.out.println("movie");
       }
    
       public static void user() {
          System.out.println("user");
       }
    
    }

  5. #5
    Join Date
    Sep 2009
    Posts
    8
    Rep Power
    0

    Default

    Thank you....

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default

    This may sound dumb after Fubarable's elegant and well written example, and because you asked for a hashmap specifically, but why not simply use a switch/case statement? Will you be adding methods to the hashmap on the fly? Because, otherwise
    Java Code:
    switch(userInput){
    case 1:book();break;
    case 2:movie();break;
    case 3:user();break;
    }
    is very simple and quite efficient compared to the insertion into a hashmap.

    If it needs to be dynamic at run time, I see the need for HM, otherwise... why not? :D

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by quad64bit View Post
    This may sound dumb after Fubarable's elegant and well written example, and because you asked for a hashmap specifically, but why not simply use a switch/case statement?
    You're right of course, as the correct answer is not always what the op asked for. Oft-times simpler is much better -- especially when it comes time to debug!

  8. #8
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Replies: 4
    Last Post: 04-03-2009, 06:36 AM
  3. hashmap
    By tOpach in forum New To Java
    Replies: 2
    Last Post: 09-24-2008, 12:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •